You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Bruno P. Kinoshita (Jira)" <ji...@apache.org> on 2020/04/13 02:52:00 UTC

[jira] [Commented] (IMAGING-132) Remove Exif, XMP and IPTC Metadata

    [ https://issues.apache.org/jira/browse/IMAGING-132?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17082000#comment-17082000 ] 

Bruno P. Kinoshita commented on IMAGING-132:
--------------------------------------------

Hi!

There are some methods for filtering metadata, but that's not easy to use them, nor find more about them, sorry.

Answering your question, I think it would be simpler to keep the SOS (star of stream) and the APP0 segment. That should give you an image without the metadata I believe.
{code:java}
package org.apache.commons.imaging.formats.jpeg.xmp;import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.common.bytesource.ByteSource;
import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
import org.apache.commons.imaging.formats.jpeg.JpegConstants;
import org.apache.commons.imaging.formats.jpeg.JpegUtils;
import org.apache.commons.imaging.formats.jpeg.xmp.JpegRewriter.JFIFPiece;
import org.apache.commons.imaging.formats.jpeg.xmp.JpegRewriter.JFIFPieceImageData;
import org.apache.commons.imaging.formats.jpeg.xmp.JpegRewriter.JFIFPieceSegment;public class RemoveMetadata {    public static void main(String[] args) throws Exception {
        File file = new File("/tmp/01.jpg");
        ByteSource byteSource = new ByteSourceFile(file);
        final List<JFIFPiece> pieces = new ArrayList<>();
        JpegUtils.Visitor visitor = new JpegUtils.Visitor() {
            @Override
            public boolean visitSegment(int marker, byte[] markerBytes, int segmentLength, byte[] segmentLengthBytes,
                    byte[] segmentData) throws ImageReadException, IOException {
                // keep only the APP0 marker
                if ("ffe0".equals(Integer.toHexString(marker))) {
                    final JFIFPiece piece = new JFIFPieceSegment(marker, markerBytes, segmentLengthBytes, segmentData);
                    pieces.add(piece);
                }
                return true;
            }

            @Override
            public void visitSOS(int marker, byte[] markerBytes, byte[] imageData) {
                pieces.add(new JFIFPieceImageData(markerBytes, imageData));
            }

            @Override
            public boolean beginSOS() {
                return true;
            }
        };
        new JpegUtils().traverseJFIF(byteSource, visitor);
        OutputStream newImage = new FileOutputStream(new File("/tmp/02.jpg"));
        try (DataOutputStream os = new DataOutputStream(newImage)) {
            JpegConstants.SOI.writeTo(os);
            for (final JFIFPiece piece : pieces) {
                piece.write(os);
            }
        }
    }
}

 {code}
I did a quick test with the code above. Image 01.jpg had 55.3 Kb, and image 02.jpg had 0.3 less. exiftool also found no metadata on image 02.jpg (01.jpg had some iptc data), only file type, resolution, jfif version, file size, name, etc. i.e. the basic info.

Feel free to ask questions over the commons user mailing list, prefixing your e-mail subject with [imaging], or post to StackOverflow (I look around every now and then, mailing list is definitely the best place for these questions).

Cheers

Bruno

> Remove Exif, XMP and IPTC Metadata
> ----------------------------------
>
>                 Key: IMAGING-132
>                 URL: https://issues.apache.org/jira/browse/IMAGING-132
>             Project: Commons Imaging
>          Issue Type: Improvement
>          Components: Format: JPEG
>    Affects Versions: 1.0-alpha1
>         Environment: Windows
>            Reporter: Jacinto Verdaguer
>            Assignee: Bruno P. Kinoshita
>            Priority: Major
>              Labels: performance
>             Fix For: Patch Needed
>
>
> I need to delete all the metadata (Exif, IPTC and XMP) of many JPEG images.
> Is there a single command to do this?
> {code:java}
> public static void removeExifMetadata(final File jpegImageFile, final File dst, boolean exif, boolean xmp, boolean iptc) throws IOException, ImageReadException, ImageWriteException {
>         OutputStream os = null;
>         boolean canThrow = false;
>         try {
>             os = new FileOutputStream(dst);
>             os = new BufferedOutputStream(os);
>             if(exif)
>                 new ExifRewriter().removeExifMetadata(jpegImageFile, os);
>             else if(iptc)
>                 new JpegIptcRewriter().removeIPTC(jpegImageFile, os);
>             else if (xmp)
>                 new JpegXmpRewriter().removeXmpXml(jpegImageFile, os);
>             canThrow = true;
>         } finally {
>             IoUtils.closeQuietly(canThrow, os);
>         }
>     }
> {code}
> This form seems too slow, involves reading and writing the file 3 times.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)