-2

JPEG has many Marker Segment Levels, I want to read and write Comment marker segment level - COM (read/write).

Description :

JPEG image has following structure :

http://help.accusoft.com/ImageGear/v18.1/Mac/IGDLL-10-05.html

In that , http://help.accusoft.com/ImageGear/v18.1/Mac/IGDLL-10-05.html#hs-inthistopic-232092be-8c07-4ae5-9c04-6cff6a52e9f6

In this all marker segment levels , i particularly want to edit COM segment , that segment is for comments.

in this segment i want to add/update/delete comment and want to retrieve all comments same question in ios :

How can i access JPEG COM segment in iOS?

i want to achieve this in ANDROID.

how to scan the JPEG stream for the COM marker, read the length, then read the data. Just be sure to get the length bytes in the right order.

how to skip over the other markers that have lengths (or fixed lengths).

how to scan for APPn, DHT, DHQ, and COM markers. Read the lengths. And skip over all but the COM marker.

Hope u guys get my question ??

Hope for ur help !!!! Thank you :-)

Stack
  • 23
  • 6

2 Answers2

0
/* EXIF & PANORAMA XMP MetaData Writer */
    IImageMetadata metadata = null;
    String XmpStringdata = null;

    JpegImageMetadata jpegMetadata = null;
    TiffImageMetadata exif = null;
    TiffOutputSet exifOutputSet = null;

private File CopyImage(String sourcepath, String targetpath) {
    File sourceLocation = new File(sourcepath);
    File targetLocation = new File(targetpath);

    InputStream in = null;
    OutputStream out = null;

    TiffImageMetadata exif = getExifXmpInfo(sourceLocation);
    try {
        in = new FileInputStream(sourceLocation);
        out = new FileOutputStream(targetLocation);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // Copy the bits from instream to outstream
    byte[] buf = new byte[1024];
    int len;
    try {
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        in.close();
        new ExifRewriter().updateExifMetadataLossless(targetLocation, out, exif);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }


    return targetLocation;
}



public TiffImageMetadata getExifXmpInfo(File sourcepath) {
    metadata = null;
    XmpStringdata = "";
    XmpStringdata = null;

    /* Get EXIF & XMP data from Image File */
    try {
        // EXIF
        metadata = Sanselan.getMetadata(sourcepath);

        // XMP
        XmpStringdata = Sanselan.getXmpXml(sourcepath);

        if (metadata == null || !(metadata instanceof JpegImageMetadata))
            // comment for no metadata get in 4.3 < device
            return null;

        jpegMetadata = (JpegImageMetadata) metadata;

        exif = jpegMetadata.getExif();

        // comment for no metadata get in 4.3 < device
        if (exif == null)
            return null;
        else
            return exif;

    } catch (ImageReadException | IOException e) {
        e.printStackTrace();
        return null;
    }
}

include jar library in gradle https://github.com/fulcrumapp/sanselan-android/releases

add extra stuff what you need

Amjad Khan
  • 1,309
  • 15
  • 32
  • can not resolve method getXmpXml , and i think this will add comment in XP comment , i want to add it in file comment – Stack Jun 01 '17 at 07:03
  • That has been removed in the latest version library, I have old one that work fine. https://www.dropbox.com/home?preview=jar_sans.jar – Amjad Khan Jun 01 '17 at 07:40
  • but this code is adding xmp comment and i want jpeg file comment access – Stack Jun 01 '17 at 08:14
0

The easiest way to read the comment is to scan the JPEG stream for the COM marker, read the length, then read the data. Just be sure to get the length bytes in the right order.

The only difficulty here is that you need to skip over the other markers that have lengths (or fixed lengths). The sequence FFFE cannot occur in the compressed data but it might occur within an APPn marker, for example.

Thus you need scan for APPn, DHT, DHQ, and COM markers. Read the lengths. And skip over all but the COM marker.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • thank you , i have one code but that just read one comment only . following is link for this : http://www.java2s.com/Code/Java/2D-Graphics-GUI/GetJpegProperties.htm , but in my case i want to add/update/delete multiple comments and get all comments. – Stack Jun 02 '17 at 04:17
  • With the JPEG stream you can insert into and delete from COM markets. To delete, just do a copy where you don't copy the COM marker. For insert, add the COM Marker after that APPn markers if present. – user3344003 Jun 02 '17 at 18:37
  • can u help me ?? how to read bytes from COM segment of jpeg file – Stack Jun 07 '17 at 05:48
  • What do you need at this point? Do you have a JPEG scanning program? Are you sure you are writing the lengths in the right byte order? – user3344003 Jun 07 '17 at 21:32
  • i am done with reading all comments from jpeg com segment , but when i try to write bytes at this com segment , i am unable to write , i think it is because of i am writing bytes at wrong place. – Stack Jun 08 '17 at 04:08
  • this is the link from where i have done reading all comments : http://www.java2s.com/Code/Java/2D-Graphics-GUI/GetJpegProperties.htm – Stack Jun 08 '17 at 04:09
  • What do you to do write? – user3344003 Jun 08 '17 at 16:07
  • i have taken one output stream and use write function , but i am finding exact position where i have to write to add comment – Stack Jun 09 '17 at 04:20