Android Open Source - PictureMap Jpeg Reader






From Project

Back to project page PictureMap.

License

The source code is released under:

GNU General Public License

If you think the Android project PictureMap listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * This is public domain software - that is, you can do whatever you want
 * with it, and include it software that is licensed under the GNU or the
 * BSD license, or whatever other licence you choose, including proprietary
 * closed source licenses.  I do ask that you leave this header in tact.
 */*from www  .j a v a  2  s  .c  o  m*/
 * If you make modifications to this code that you think would benefit the
 * wider community, please send me a copy and I'll post it on my site.
 *
 * If you make use of this code, I'd appreciate hearing about it.
 *   drew@drewnoakes.com
 * Latest version of this software kept at
 *   http://drewnoakes.com/
 *
 * Created by dnoakes on Aug 2, 2003 using IntelliJ IDEA.
 */
package com.drewChanged.metadata.jpeg;

import com.drewChanged.imaging.jpeg.JpegProcessingException;
import com.drewChanged.imaging.jpeg.JpegSegmentReader;
import com.drewChanged.metadata.Metadata;
import com.drewChanged.metadata.MetadataException;
import com.drewChanged.metadata.MetadataReader;

//import java.io.File;
import java.io.InputStream;

/**
 *
 * @author Darrell Silver http://www.darrellsilver.com and Drew Noakes
 */
public class JpegReader implements MetadataReader
{
    /**
     * The SOF0 data segment.
     */
    private final byte[] _data;

    /**
     * Creates a new JpegReader for the specified Jpeg jpegFile.
     */
//    public JpegReader(File jpegFile) throws JpegProcessingException
//    {
//        this(new JpegSegmentReader(jpegFile).readSegment(JpegSegmentReader.SEGMENT_SOF0));
//    }

    /** Creates a JpegReader for a JPEG stream.
     *
     * @param is JPEG stream. Stream will be closed.
     */
    public JpegReader(InputStream is) throws JpegProcessingException
    {
        this(new JpegSegmentReader(is).readSegment(JpegSegmentReader.SEGMENT_APPD));
    }

    public JpegReader(byte[] data)
    {
        _data = data;
    }

    /**
     * Performs the Jpeg data extraction, returning a new instance of <code>Metadata</code>.
     */
    public Metadata extract()
    {
        return extract(new Metadata());
    }

    /**
     * Performs the Jpeg data extraction, adding found values to the specified
     * instance of <code>Metadata</code>.
     */
    public Metadata extract(Metadata metadata)
    {
        if (_data==null) {
            return metadata;
        }

        JpegDirectory directory = (JpegDirectory)metadata.getDirectory(JpegDirectory.class);
//        System.out.println("directory is obtained! "+directory.getName());
        try {
            // data precision
            int dataPrecision = get16Bits(JpegDirectory.TAG_JPEG_DATA_PRECISION);
            directory.setInt(JpegDirectory.TAG_JPEG_DATA_PRECISION, dataPrecision);

            // process height
            int height = get32Bits(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);
            directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT, height);

            // process width
            int width = get32Bits(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);
            directory.setInt(JpegDirectory.TAG_JPEG_IMAGE_WIDTH, width);

            // number of components
            int numberOfComponents = get16Bits(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS);
            directory.setInt(JpegDirectory.TAG_JPEG_NUMBER_OF_COMPONENTS, numberOfComponents);

            // for each component, there are three bytes of data:
            // 1 - Component ID: 1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q
            // 2 - Sampling factors: bit 0-3 vertical, 4-7 horizontal
            // 3 - Quantization table number
            int offset = 6;
            for (int i=0; i<numberOfComponents; i++)
            {
                int componentId = get16Bits(offset++);
                int samplingFactorByte = get16Bits(offset++);
                int quantizationTableNumber = get16Bits(offset++);
                JpegComponent component = new JpegComponent(componentId, samplingFactorByte, quantizationTableNumber);
                directory.setObject(JpegDirectory.TAG_JPEG_COMPONENT_DATA_1 + i, component);
//                System.out.println("Just set object JpegComponent: "+componentId +"," +samplingFactorByte+"," +quantizationTableNumber);
            }

        } catch (MetadataException me) {
//            System.out.println("MetadataException: " + me);
        }

        return metadata;
    }

    /**
     * Returns an int calculated from two bytes of data at the specified offset (MSB, LSB).
     * @param offset position within the data buffer to read first byte
     * @return the 32 bit int value, between 0x0000 and 0xFFFF
     */
    private int get32Bits(int offset) throws MetadataException
    {
        if (offset+1>=_data.length) {
            throw new MetadataException("Attempt to read bytes from outside Jpeg segment data buffer");
        }

        return ((_data[offset] & 255) << 8) | (_data[offset + 1] & 255);
    }

    /**
     * Returns an int calculated from one byte of data at the specified offset.
     * @param offset position within the data buffer to read byte
     * @return the 16 bit int value, between 0x00 and 0xFF
     */
    private int get16Bits(int offset) throws MetadataException
    {
        if (offset>=_data.length) {
            throw new MetadataException("Attempt to read bytes from outside Jpeg segment data buffer");
        }

        return (_data[offset] & 255);
    }
}




Java Source Code List

com.aripollak.picturemap.ImageUtilities.java
com.aripollak.picturemap.MainActivity.java
com.aripollak.picturemap.PictureCallout.java
com.aripollak.picturemap.PopulateMapTask.java
com.drewChanged.imaging.PhotographicConversions.java
com.drewChanged.imaging.jpeg.JpegMetadataReader.java
com.drewChanged.imaging.jpeg.JpegProcessingException.java
com.drewChanged.imaging.jpeg.JpegSegmentData.java
com.drewChanged.imaging.jpeg.JpegSegmentReader.java
com.drewChanged.lang.CompoundException.java
com.drewChanged.lang.NullOutputStream.java
com.drewChanged.lang.Rational.java
com.drewChanged.metadata.DefaultTagDescriptor.java
com.drewChanged.metadata.Directory.java
com.drewChanged.metadata.MetadataException.java
com.drewChanged.metadata.MetadataReader.java
com.drewChanged.metadata.Metadata.java
com.drewChanged.metadata.TagDescriptor.java
com.drewChanged.metadata.Tag.java
com.drewChanged.metadata.exif.DataFormat.java
com.drewChanged.metadata.exif.ExifDescriptor.java
com.drewChanged.metadata.exif.ExifDirectory.java
com.drewChanged.metadata.exif.ExifInteropDescriptor.java
com.drewChanged.metadata.exif.ExifInteropDirectory.java
com.drewChanged.metadata.exif.ExifProcessingException.java
com.drewChanged.metadata.exif.ExifReader.java
com.drewChanged.metadata.exif.GpsDescriptor.java
com.drewChanged.metadata.exif.GpsDirectory.java
com.drewChanged.metadata.iptc.IptcDescriptor.java
com.drewChanged.metadata.iptc.IptcDirectory.java
com.drewChanged.metadata.iptc.IptcProcessingException.java
com.drewChanged.metadata.iptc.IptcReader.java
com.drewChanged.metadata.jpeg.JpegCommentDescriptor.java
com.drewChanged.metadata.jpeg.JpegCommentDirectory.java
com.drewChanged.metadata.jpeg.JpegCommentReader.java
com.drewChanged.metadata.jpeg.JpegComponent.java
com.drewChanged.metadata.jpeg.JpegDescriptor.java
com.drewChanged.metadata.jpeg.JpegDirectory.java
com.drewChanged.metadata.jpeg.JpegReader.java