4

I'm using apache-commons-sanselan.jar API to remove EXIF content from only JPEG file.

How to remove this content from other file extensions?

0m3r
  • 12,286
  • 15
  • 35
  • 71
Puneeth Reddy V
  • 1,538
  • 13
  • 28
  • Related: http://stackoverflow.com/questions/37062959/how-to-remove-image-metadata-from-large-images-without-out-of-memory-in-java – Thilo May 06 '16 at 01:55

2 Answers2

10
BufferedImage image = ImageIO.read(new File("image.jpg"));  
ImageIO.write(image, "jpg", new File("image.jpg"));  

Metadata isn't read when you read an image. Just write it back. Replace jpg with the extension you want.

Sources:
How to remove Exif,IPTC,XMP data of a png image in Java
How can I remove metadata from a JPEG image in Java?

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
-1

In addition to @little-child answer

code:

public static void removeExifTag(final String sourceImageFile, final File destinationImageFile) throws IOException, ImageReadException, ImageWriteException {
    try (
            OutputStream os = new FileOutputStream(destinationImageFile);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ){
        BufferedImage originalImage = ImageIO.read(new File(sourceImageFile));
        originalImage.flush();
        ImageIO.write( originalImage,"jpg", baos );
        byte[] imageInByte = baos.toByteArray();
        new ExifRewriter().removeExifMetadata(imageInByte, bos);
        baos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }  catch (IOException e) {
        e.printStackTrace();
    } catch (ImageReadException e) {
        e.printStackTrace();
    } catch (ImageWriteException e) {
        e.printStackTrace();
    } 
}
Community
  • 1
  • 1
Puneeth Reddy V
  • 1,538
  • 13
  • 28
  • doesn't works with png images cus it throws org.apache.commons.imaging.ImageReadException: Not a Valid JPEG File: doesn't begin with 0xffd8 – gori Dec 01 '17 at 12:02
  • what are the arguments you are passing to .write method of `ImageIO` object? – Puneeth Reddy V Dec 01 '17 at 14:26
  • "png", but this is working actually the line that is broken is ```new ExifRewriter().removeExifMetadata(imageInByte, bos);``` but if working actually your code is faster that the one of @Little Child – gori Dec 01 '17 at 14:35