Example usage for org.apache.commons.imaging.formats.jpeg.exif ExifRewriter updateExifMetadataLossy

List of usage examples for org.apache.commons.imaging.formats.jpeg.exif ExifRewriter updateExifMetadataLossy

Introduction

In this page you can find the example usage for org.apache.commons.imaging.formats.jpeg.exif ExifRewriter updateExifMetadataLossy.

Prototype

public void updateExifMetadataLossy(final ByteSource byteSource, final OutputStream os,
        final TiffOutputSet outputSet) throws ImageReadException, IOException, ImageWriteException 

Source Link

Document

Reads a Jpeg image, replaces the EXIF metadata and writes the result to a stream.

Usage

From source file:com.hygenics.imaging.ImageCleanup.java

public byte[] writeMetaData(String data, byte[] ibytes) {
    // write metadata based on the metadata columns list
    TiffOutputSet outset = null;//ww  w.j ava 2 s .  c  om
    BufferedImage bi;
    ImageMetadata metadata;
    ByteArrayOutputStream bos = null;
    try {

        // get the buffered image to write to
        bi = Imaging.getBufferedImage(ibytes);
        metadata = Imaging.getMetadata(ibytes);
        JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

        if (null != jpegMetadata) {
            // get the image exif data
            TiffImageMetadata exif = jpegMetadata.getExif();
            outset = exif.getOutputSet();
        }

        if (outset == null) {
            // get a new set (directory structured to write to)
            outset = new TiffOutputSet();
        }

        TiffOutputDirectory exdir = outset.getOrCreateExifDirectory();
        exdir.removeField(ExifTagConstants.EXIF_TAG_USER_COMMENT);

        exdir.add(ExifTagConstants.EXIF_TAG_USER_COMMENT, data.trim());

        bos = new ByteArrayOutputStream();
        ByteArrayInputStream bis = new ByteArrayInputStream(ibytes);

        ExifRewriter exrw = new ExifRewriter();

        // read to a byte stream
        exrw.updateExifMetadataLossy(bis, bos, outset);

        // read the input from the byte buffer
        ibytes = bos.toByteArray();
        bis.close();
        bos.close();

    } catch (ImageReadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ImageWriteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return ibytes;
}

From source file:com.ubb.imaging.ExifMetadataWriter.java

public void modifyXpTitle(File fileIn, File fileOut, String newValue) throws Exception {
    TiffImageMetadata exif;// ww  w  .  j a  va2s  .c o  m
    ImageMetadata meta = Imaging.getMetadata(fileIn);
    if (meta instanceof JpegImageMetadata) {
        exif = ((JpegImageMetadata) meta).getExif();
    } else if (meta instanceof TiffImageMetadata) {
        exif = (TiffImageMetadata) meta;
    } else {
        return;
    }
    TiffOutputSet outputSet = exif.getOutputSet();
    TiffOutputDirectory exifDir = outputSet.findDirectory(TiffDirectoryConstants.DIRECTORY_TYPE_EXIF);
    exifDir.removeField(AllTagConstants.EXIF_TAG_XPTITLE);
    exifDir.add(AllTagConstants.EXIF_TAG_XPTITLE, newValue);

    ExifRewriter rewriter = new ExifRewriter();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(fileOut);
        rewriter.updateExifMetadataLossy(fileIn, fos, outputSet);
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
}