Search in sources :

Example 11 with ExifInterface

use of android.media.ExifInterface in project platform_frameworks_base by android.

the class ExifInterfaceTest method testSaveAttributes_withFileName.

private void testSaveAttributes_withFileName(File imageFile, ExpectedValue expectedValue) throws IOException {
    String verboseTag = imageFile.getName();
    ExifInterface exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    exifInterface.saveAttributes();
    exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    // Test for modifying one attribute.
    String backupValue = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
    exifInterface.setAttribute(ExifInterface.TAG_MAKE, "abc");
    exifInterface.saveAttributes();
    exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    assertEquals("abc", exifInterface.getAttribute(ExifInterface.TAG_MAKE));
    // Restore the backup value.
    exifInterface.setAttribute(ExifInterface.TAG_MAKE, backupValue);
    exifInterface.saveAttributes();
    exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
}
Also used : ExifInterface(android.media.ExifInterface)

Example 12 with ExifInterface

use of android.media.ExifInterface in project platform_frameworks_base by android.

the class ExifInterfaceTest method testSaveAttributes_withFileDescriptor.

private void testSaveAttributes_withFileDescriptor(File imageFile, ExpectedValue expectedValue) throws IOException {
    String verboseTag = imageFile.getName();
    FileDescriptor fd = null;
    try {
        fd = Os.open(imageFile.getAbsolutePath(), OsConstants.O_RDWR, 0600);
        ExifInterface exifInterface = new ExifInterface(fd);
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
        // Test for modifying one attribute.
        String backupValue = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
        exifInterface.setAttribute(ExifInterface.TAG_MAKE, "abc");
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        assertEquals("abc", exifInterface.getAttribute(ExifInterface.TAG_MAKE));
        // Restore the backup value.
        exifInterface.setAttribute(ExifInterface.TAG_MAKE, backupValue);
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(fd);
    }
}
Also used : ErrnoException(android.system.ErrnoException) ExifInterface(android.media.ExifInterface) FileDescriptor(java.io.FileDescriptor)

Example 13 with ExifInterface

use of android.media.ExifInterface in project platform_frameworks_base by android.

the class ExifInterfaceTest method testExifInterfaceCommon.

private void testExifInterfaceCommon(File imageFile, ExpectedValue expectedValue) throws IOException {
    String verboseTag = imageFile.getName();
    // Creates via path.
    ExifInterface exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    // Creates from an asset file.
    InputStream in = null;
    try {
        in = mContext.getAssets().open(imageFile.getName());
        exifInterface = new ExifInterface(in);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } finally {
        IoUtils.closeQuietly(in);
    }
    // Creates via InputStream.
    in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(imageFile.getAbsolutePath()));
        exifInterface = new ExifInterface(in);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } finally {
        IoUtils.closeQuietly(in);
    }
    // Creates via FileDescriptor.
    FileDescriptor fd = null;
    try {
        fd = Os.open(imageFile.getAbsolutePath(), OsConstants.O_RDONLY, 0600);
        exifInterface = new ExifInterface(fd);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(fd);
    }
}
Also used : ErrnoException(android.system.ErrnoException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExifInterface(android.media.ExifInterface) FileInputStream(java.io.FileInputStream) FileDescriptor(java.io.FileDescriptor)

Example 14 with ExifInterface

use of android.media.ExifInterface in project platform_frameworks_base by android.

the class CameraTestUtils method verifyJpegKeys.

/**
     * Verify the JPEG EXIF and JPEG related keys in a capture result are expected.
     * - Capture request get values are same as were set.
     * - capture result's exif data is the same as was set by
     *   the capture request.
     * - new tags in the result set by the camera service are
     *   present and semantically correct.
     *
     * @param image The output JPEG image to verify.
     * @param captureResult The capture result to verify.
     * @param expectedSize The expected JPEG size.
     * @param expectedThumbnailSize The expected thumbnail size.
     * @param expectedExifData The expected EXIF data
     * @param staticInfo The static metadata for the camera device.
     * @param jpegFilename The filename to dump the jpeg to.
     * @param collector The camera error collector to collect errors.
     */
public static void verifyJpegKeys(Image image, CaptureResult captureResult, Size expectedSize, Size expectedThumbnailSize, ExifTestData expectedExifData, StaticMetadata staticInfo, CameraErrorCollector collector) throws Exception {
    basicValidateJpegImage(image, expectedSize);
    byte[] jpegBuffer = getDataFromImage(image);
    // Have to dump into a file to be able to use ExifInterface
    String jpegFilename = DEBUG_FILE_NAME_BASE + "/verifyJpegKeys.jpeg";
    dumpFile(jpegFilename, jpegBuffer);
    ExifInterface exif = new ExifInterface(jpegFilename);
    if (expectedThumbnailSize.equals(new Size(0, 0))) {
        collector.expectTrue("Jpeg shouldn't have thumbnail when thumbnail size is (0, 0)", !exif.hasThumbnail());
    } else {
        collector.expectTrue("Jpeg must have thumbnail for thumbnail size " + expectedThumbnailSize, exif.hasThumbnail());
    }
    // Validate capture result vs. request
    Size resultThumbnailSize = captureResult.get(CaptureResult.JPEG_THUMBNAIL_SIZE);
    int orientationTested = expectedExifData.jpegOrientation;
    // Legacy shim always doesn't rotate thumbnail size
    if ((orientationTested == 90 || orientationTested == 270) && staticInfo.isHardwareLevelLimitedOrBetter()) {
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, /*defaultValue*/
        -1);
        if (exifOrientation == ExifInterface.ORIENTATION_UNDEFINED) {
            // Device physically rotated image+thumbnail data
            // Expect thumbnail size to be also rotated
            resultThumbnailSize = new Size(resultThumbnailSize.getHeight(), resultThumbnailSize.getWidth());
        }
    }
    collector.expectEquals("JPEG thumbnail size result and request should match", expectedThumbnailSize, resultThumbnailSize);
    if (collector.expectKeyValueNotNull(captureResult, CaptureResult.JPEG_GPS_LOCATION) != null) {
        collector.expectTrue("GPS location result and request should match.", areGpsFieldsEqual(expectedExifData.gpsLocation, captureResult.get(CaptureResult.JPEG_GPS_LOCATION)));
    }
    collector.expectEquals("JPEG orientation result and request should match", expectedExifData.jpegOrientation, captureResult.get(CaptureResult.JPEG_ORIENTATION));
    collector.expectEquals("JPEG quality result and request should match", expectedExifData.jpegQuality, captureResult.get(CaptureResult.JPEG_QUALITY));
    collector.expectEquals("JPEG thumbnail quality result and request should match", expectedExifData.thumbnailQuality, captureResult.get(CaptureResult.JPEG_THUMBNAIL_QUALITY));
    // Validate other exif tags for all non-legacy devices
    if (!staticInfo.isHardwareLevelLegacy()) {
        verifyJpegExifExtraTags(exif, expectedSize, captureResult, staticInfo, collector);
    }
}
Also used : Size(android.util.Size) ExifInterface(android.media.ExifInterface)

Example 15 with ExifInterface

use of android.media.ExifInterface in project react-native-image-picker by marcshilling.

the class ImagePickerModule method getResizedImage.

/**
   * Create a resized image to fulfill the maxWidth/maxHeight, quality and rotation values
   *
   * @param realPath
   * @param initialWidth
   * @param initialHeight
   * @return resized file
   */
private File getResizedImage(final String realPath, final int initialWidth, final int initialHeight) {
    Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap photo = BitmapFactory.decodeFile(realPath, options);
    if (photo == null) {
        return null;
    }
    Bitmap scaledphoto = null;
    if (maxWidth == 0 || maxWidth > initialWidth) {
        maxWidth = initialWidth;
    }
    if (maxHeight == 0 || maxWidth > initialHeight) {
        maxHeight = initialHeight;
    }
    double widthRatio = (double) maxWidth / initialWidth;
    double heightRatio = (double) maxHeight / initialHeight;
    double ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio;
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation);
    matrix.postScale((float) ratio, (float) ratio);
    ExifInterface exif;
    try {
        exif = new ExifInterface(realPath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
        if (orientation == 6) {
            matrix.postRotate(90);
        } else if (orientation == 3) {
            matrix.postRotate(180);
        } else if (orientation == 8) {
            matrix.postRotate(270);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    scaledphoto = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    scaledphoto.compress(Bitmap.CompressFormat.JPEG, quality, bytes);
    File f = createNewFile();
    FileOutputStream fo;
    try {
        fo = new FileOutputStream(f);
        try {
            fo.write(bytes.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // recycle to avoid java.lang.OutOfMemoryError
    if (photo != null) {
        scaledphoto.recycle();
        photo.recycle();
        scaledphoto = null;
        photo = null;
    }
    return f;
}
Also used : Options(android.graphics.BitmapFactory.Options) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) ExifInterface(android.media.ExifInterface) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Aggregations

ExifInterface (android.media.ExifInterface)153 IOException (java.io.IOException)93 Paint (android.graphics.Paint)32 Bitmap (android.graphics.Bitmap)26 InputStream (java.io.InputStream)21 File (java.io.File)17 BitmapFactory (android.graphics.BitmapFactory)16 Matrix (android.graphics.Matrix)15 FileInputStream (java.io.FileInputStream)15 Point (android.graphics.Point)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ErrnoException (android.system.ErrnoException)10 BufferedInputStream (java.io.BufferedInputStream)10 FileDescriptor (java.io.FileDescriptor)10 FileNotFoundException (java.io.FileNotFoundException)10 SuppressLint (android.annotation.SuppressLint)9 FileOutputStream (java.io.FileOutputStream)8 Cursor (android.database.Cursor)7 AssetFileDescriptor (android.content.res.AssetFileDescriptor)6 Bundle (android.os.Bundle)5