Search in sources :

Example 11 with ExifInterface

use of com.android.gallery3d.exif.ExifInterface in project android_packages_apps_Gallery2 by LineageOS.

the class MediaDetails method extractExifInfo.

public static void extractExifInfo(MediaDetails details, String filePath) {
    ExifInterface exif = new ExifInterface();
    try {
        exif.readExif(filePath);
    } catch (FileNotFoundException e) {
        Log.w(TAG, "Could not find file to read exif: " + filePath, e);
    } catch (IOException e) {
        Log.w(TAG, "Could not read exif from file: " + filePath, e);
    }
    setExifData(details, exif.getTag(ExifInterface.TAG_FLASH), MediaDetails.INDEX_FLASH);
    setExifData(details, exif.getTag(ExifInterface.TAG_IMAGE_WIDTH), MediaDetails.INDEX_WIDTH);
    setExifData(details, exif.getTag(ExifInterface.TAG_IMAGE_LENGTH), MediaDetails.INDEX_HEIGHT);
    ExifTag recordTag = exif.getTag(ExifInterface.TAG_DATE_TIME_ORIGINAL);
    if (recordTag == null)
        recordTag = exif.getTag(ExifInterface.TAG_DATE_TIME_DIGITIZED);
    if (recordTag == null)
        recordTag = exif.getTag(ExifInterface.TAG_DATE_TIME);
    setExifData(details, recordTag, MediaDetails.INDEX_DATETIME_ORIGINAL);
    setExifData(details, exif.getTag(ExifInterface.TAG_MAKE), MediaDetails.INDEX_MAKE);
    setExifData(details, exif.getTag(ExifInterface.TAG_MODEL), MediaDetails.INDEX_MODEL);
    setExifData(details, exif.getTag(ExifInterface.TAG_APERTURE_VALUE), MediaDetails.INDEX_APERTURE);
    setExifData(details, exif.getTag(ExifInterface.TAG_ISO_SPEED_RATINGS), MediaDetails.INDEX_ISO);
    setExifData(details, exif.getTag(ExifInterface.TAG_WHITE_BALANCE), MediaDetails.INDEX_WHITE_BALANCE);
    setExifData(details, exif.getTag(ExifInterface.TAG_EXPOSURE_TIME), MediaDetails.INDEX_EXPOSURE_TIME);
    ExifTag focalTag = exif.getTag(ExifInterface.TAG_FOCAL_LENGTH);
    if (focalTag != null) {
        details.addDetail(MediaDetails.INDEX_FOCAL_LENGTH, focalTag.getValueAsRational(0).toDouble());
        details.setUnit(MediaDetails.INDEX_FOCAL_LENGTH, R.string.unit_mm);
    }
}
Also used : ExifTag(com.android.gallery3d.exif.ExifTag) ExifInterface(com.android.gallery3d.exif.ExifInterface) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 12 with ExifInterface

use of com.android.gallery3d.exif.ExifInterface in project android_packages_apps_Gallery2 by LineageOS.

the class SaveImage method processAndSaveImage.

public Uri processAndSaveImage(ImagePreset preset, boolean flatten, int quality, float sizeFactor, boolean exit) {
    Uri uri = null;
    if (exit) {
        uri = resetToOriginalImageIfNeeded(preset, !flatten);
    }
    if (uri != null) {
        return null;
    }
    resetProgress();
    boolean noBitmap = true;
    int num_tries = 0;
    int sampleSize = 1;
    // If necessary, move the source file into the auxiliary directory,
    // newSourceUri is then pointing to the new location.
    // If no file is moved, newSourceUri will be the same as mSourceUri.
    Uri newSourceUri = mSourceUri;
    /*
         * if (!flatten) { newSourceUri = moveSrcToAuxIfNeeded(mSourceUri, mDestinationFile); }
         */
    Uri savedUri = mSelectedImageUri;
    if (mPreviewImage != null) {
        Bitmap previewBmp;
        // Check for Fusion
        FilterFusionRepresentation fusionRep = findFusionRepresentation(preset);
        boolean hasFusion = (fusionRep != null && fusionRep.hasUnderlay());
        if (hasFusion) {
            previewBmp = flattenFusion(mContext, Uri.parse(fusionRep.getUnderlay()), mPreviewImage, Math.max(mPreviewImage.getWidth(), mPreviewImage.getHeight()), 0);
            // If we fail to flatten, save original image
            if (previewBmp == null) {
                previewBmp = mPreviewImage;
                hasFusion = false;
            }
        } else {
            previewBmp = mPreviewImage;
        }
        if (flatten) {
            Object xmp = getPanoramaXMPData(newSourceUri, preset);
            ExifInterface exif = getExifData(newSourceUri);
            long time = System.currentTimeMillis();
            updateExifData(exif, time);
            if (putExifData(mDestinationFile, exif, previewBmp, quality)) {
                putPanoramaXMPData(mDestinationFile, xmp);
                ContentValues values = getContentValues(mContext, mSelectedImageUri, mDestinationFile, time);
                Object result = mContext.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
                Log.d(GalleryActivity.QSST, "image exported successfully");
            }
        } else {
            Object xmp = getPanoramaXMPData(newSourceUri, preset);
            ExifInterface exif = getExifData(newSourceUri);
            long time = System.currentTimeMillis();
            updateExifData(exif, time);
            // If we succeed in writing the bitmap as a jpeg, return a uri.
            if (putExifData(mDestinationFile, exif, previewBmp, quality)) {
                putPanoramaXMPData(mDestinationFile, xmp);
                // mDestinationFile will save the newSourceUri info in the XMP.
                if (!flatten) {
                    XmpPresets.writeFilterXMP(mContext, newSourceUri, mDestinationFile, preset);
                }
                // After this call, mSelectedImageUri will be actually
                // pointing at the new file mDestinationFile.
                savedUri = SaveImage.linkNewFileToUri(mContext, mSelectedImageUri, mDestinationFile, time, false);
            }
        }
        if (hasFusion) {
            // recycle bitmap
            previewBmp.recycle();
            previewBmp = null;
        }
        if (mCallback != null) {
            mCallback.onPreviewSaved(savedUri);
        }
    }
    // Stopgap fix for low-memory devices.
    while (noBitmap) {
        try {
            updateProgress();
            // Try to do bitmap operations, downsample if low-memory
            Bitmap bitmap = ImageLoader.loadOrientedBitmapWithBackouts(mContext, newSourceUri, sampleSize);
            if (bitmap == null) {
                return null;
            }
            if (sizeFactor != 1f) {
                // if we have a valid size
                int w = (int) (bitmap.getWidth() * sizeFactor);
                int h = (int) (bitmap.getHeight() * sizeFactor);
                if (w == 0 || h == 0) {
                    w = 1;
                    h = 1;
                }
                bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
            }
            updateProgress();
            CachingPipeline pipeline = new CachingPipeline(FiltersManager.getManager(), "Saving");
            bitmap = pipeline.renderFinalImage(bitmap, preset);
            // Check for Fusion
            FilterFusionRepresentation fusionRep = findFusionRepresentation(preset);
            boolean hasFusion = (fusionRep != null && fusionRep.hasUnderlay());
            if (hasFusion) {
                Bitmap underlay = flattenFusion(mContext, Uri.parse(fusionRep.getUnderlay()), bitmap, 0, sampleSize);
                if (underlay != null) {
                    bitmap.recycle();
                    bitmap = underlay;
                }
            }
            updateProgress();
            Object xmp = getPanoramaXMPData(newSourceUri, preset);
            ExifInterface exif = getExifData(newSourceUri);
            long time = System.currentTimeMillis();
            updateProgress();
            updateExifData(exif, time);
            updateProgress();
            // If we succeed in writing the bitmap as a jpeg, return a uri.
            if (putExifData(mDestinationFile, exif, bitmap, quality)) {
                putPanoramaXMPData(mDestinationFile, xmp);
                // mDestinationFile will save the newSourceUri info in the XMP.
                if (!flatten) {
                    XmpPresets.writeFilterXMP(mContext, newSourceUri, mDestinationFile, preset);
                    uri = updateFile(mContext, savedUri, mDestinationFile, time);
                } else {
                    ContentValues values = getContentValues(mContext, mSelectedImageUri, mDestinationFile, time);
                    Object result = mContext.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
                }
            }
            updateProgress();
            noBitmap = false;
        } catch (OutOfMemoryError e) {
            // Try 5 times before failing for good.
            if (++num_tries >= 5) {
                throw e;
            }
            System.gc();
            sampleSize *= 2;
            resetProgress();
        }
    }
    return uri;
}
Also used : ContentValues(android.content.ContentValues) Bitmap(android.graphics.Bitmap) FilterFusionRepresentation(com.android.gallery3d.filtershow.filters.FilterFusionRepresentation) ExifInterface(com.android.gallery3d.exif.ExifInterface) Uri(android.net.Uri) Paint(android.graphics.Paint) CachingPipeline(com.android.gallery3d.filtershow.pipeline.CachingPipeline)

Aggregations

ExifInterface (com.android.gallery3d.exif.ExifInterface)12 IOException (java.io.IOException)11 InputStream (java.io.InputStream)7 BufferedInputStream (java.io.BufferedInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Uri (android.net.Uri)3 ExifTag (com.android.gallery3d.exif.ExifTag)3 FileNotFoundException (java.io.FileNotFoundException)3 ContentValues (android.content.ContentValues)2 Paint (android.graphics.Paint)2 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 Bitmap (android.graphics.Bitmap)1 FilterFusionRepresentation (com.android.gallery3d.filtershow.filters.FilterFusionRepresentation)1 CachingPipeline (com.android.gallery3d.filtershow.pipeline.CachingPipeline)1 File (java.io.File)1