Search in sources :

Example 1 with ImageConfig

use of com.imagepicker.media.ImageConfig in project react-native-image-picker by marcshilling.

the class MediaUtils method rolloutPhotoFromCamera.

@Nullable
public static RolloutPhotoResult rolloutPhotoFromCamera(@NonNull final ImageConfig imageConfig) {
    RolloutPhotoResult result = null;
    final File oldFile = imageConfig.resized == null ? imageConfig.original : imageConfig.resized;
    final File newDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    final File newFile = new File(newDir.getPath(), oldFile.getName());
    try {
        moveFile(oldFile, newFile);
        ImageConfig newImageConfig;
        if (imageConfig.resized != null) {
            newImageConfig = imageConfig.withResizedFile(newFile);
        } else {
            newImageConfig = imageConfig.withOriginalFile(newFile);
        }
        result = new RolloutPhotoResult(newImageConfig, null);
    } catch (IOException e) {
        e.printStackTrace();
        result = new RolloutPhotoResult(imageConfig, e);
    }
    return result;
}
Also used : ImageConfig(com.imagepicker.media.ImageConfig) IOException(java.io.IOException) File(java.io.File) Nullable(android.support.annotation.Nullable)

Example 2 with ImageConfig

use of com.imagepicker.media.ImageConfig in project react-native-image-picker by marcshilling.

the class MediaUtils method getResizedImage.

/**
 * Create a resized image to fulfill the maxWidth/maxHeight, quality and rotation values
 *
 * @param context
 * @param options
 * @param imageConfig
 * @param initialWidth
 * @param initialHeight
 * @return updated ImageConfig
 */
@NonNull
public static ImageConfig getResizedImage(@NonNull final Context context, @NonNull final ReadableMap options, @NonNull final ImageConfig imageConfig, int initialWidth, int initialHeight, final int requestCode) {
    BitmapFactory.Options imageOptions = new BitmapFactory.Options();
    imageOptions.inScaled = false;
    imageOptions.inSampleSize = 1;
    if (imageConfig.maxWidth != 0 || imageConfig.maxHeight != 0) {
        while ((imageConfig.maxWidth == 0 || initialWidth > 2 * imageConfig.maxWidth) && (imageConfig.maxHeight == 0 || initialHeight > 2 * imageConfig.maxHeight)) {
            imageOptions.inSampleSize *= 2;
            initialHeight /= 2;
            initialWidth /= 2;
        }
    }
    Bitmap photo = BitmapFactory.decodeFile(imageConfig.original.getAbsolutePath(), imageOptions);
    if (photo == null) {
        return null;
    }
    ImageConfig result = imageConfig;
    Bitmap scaledPhoto = null;
    if (imageConfig.maxWidth == 0 || imageConfig.maxWidth > initialWidth) {
        result = result.withMaxWidth(initialWidth);
    }
    if (imageConfig.maxHeight == 0 || imageConfig.maxWidth > initialHeight) {
        result = result.withMaxHeight(initialHeight);
    }
    double widthRatio = (double) result.maxWidth / initialWidth;
    double heightRatio = (double) result.maxHeight / initialHeight;
    double ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio;
    Matrix matrix = new Matrix();
    matrix.postRotate(result.rotation);
    matrix.postScale((float) ratio, (float) ratio);
    ExifInterface exif;
    try {
        exif = new ExifInterface(result.original.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
        switch(orientation) {
            case 6:
                matrix.postRotate(90);
                break;
            case 3:
                matrix.postRotate(180);
                break;
            case 8:
                matrix.postRotate(270);
                break;
        }
    } 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, result.quality, bytes);
    final boolean forceLocal = requestCode == REQUEST_LAUNCH_IMAGE_CAPTURE;
    final File resized = createNewFile(context, options, !forceLocal);
    if (resized == null) {
        if (photo != null) {
            photo.recycle();
            photo = null;
        }
        if (scaledPhoto != null) {
            scaledPhoto.recycle();
            scaledPhoto = null;
        }
        return imageConfig;
    }
    result = result.withResizedFile(resized);
    try (FileOutputStream fos = new FileOutputStream(result.resized)) {
        bytes.writeTo(fos);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (photo != null) {
        photo.recycle();
        photo = null;
    }
    if (scaledPhoto != null) {
        scaledPhoto.recycle();
        scaledPhoto = null;
    }
    return result;
}
Also used : ImageConfig(com.imagepicker.media.ImageConfig) ExifInterface(android.media.ExifInterface) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) FileOutputStream(java.io.FileOutputStream) BitmapFactory(android.graphics.BitmapFactory) File(java.io.File) NonNull(android.support.annotation.NonNull)

Example 3 with ImageConfig

use of com.imagepicker.media.ImageConfig in project react-native-image-picker by marcshilling.

the class ImageConfigTest method testUseOriginal.

@Test
public void testUseOriginal() {
    ImageConfig config = new ImageConfig(null, null, 800, 600, 100, 90, false);
    assertEquals("Image wont be resized", true, config.useOriginal(100, 100, 90));
    assertEquals("Image will be resized because of rotation", false, config.useOriginal(100, 100, 80));
    assertEquals("Image will be resized because of initial width", false, config.useOriginal(1000, 100, 80));
    assertEquals("Image will be resized because of initial height", false, config.useOriginal(100, 1000, 80));
    ImageConfig qualityIsLow = config.withQuality(90);
    assertEquals("Image will be resized because of quality is low", false, qualityIsLow.useOriginal(100, 100, 90));
}
Also used : ImageConfig(com.imagepicker.media.ImageConfig) Test(org.junit.Test)

Example 4 with ImageConfig

use of com.imagepicker.media.ImageConfig in project react-native-image-picker by marcshilling.

the class ImageConfigTest method testOnImmutable.

@Test
public void testOnImmutable() {
    ImageConfig original = new ImageConfig(new File("original.txt"), new File("resized.txt"), 0, 0, 0, 0, false);
    ImageConfig updated = original.withOriginalFile(null);
    assertNotNull("Original has got original file", original.original);
    assertNull("Updated hasn't got original file", updated.original);
    updated = original.withResizedFile(null);
    assertNotNull("Original has got resized file", original.resized);
    assertNull("Updated hasn't got resized file", updated.resized);
    updated = original.withMaxWidth(1);
    assertEquals("Original max width", 0, original.maxWidth);
    assertEquals("Updated max width", 1, updated.maxWidth);
    updated = original.withMaxHeight(2);
    assertEquals("Original max height", 0, original.maxHeight);
    assertEquals("Updated max height", 2, updated.maxHeight);
    updated = original.withQuality(29);
    assertEquals("Original quality", 0, original.quality);
    assertEquals("Updated quality", 29, updated.quality);
    updated = original.withRotation(135);
    assertEquals("Original rotation", 0, original.rotation);
    assertEquals("Updated rotation", 135, updated.rotation);
    updated = original.withSaveToCameraRoll(true);
    assertEquals("Original saveToCameraRoll", false, original.saveToCameraRoll);
    assertEquals("Updated saveToCameraRoll", true, updated.saveToCameraRoll);
}
Also used : ImageConfig(com.imagepicker.media.ImageConfig) File(java.io.File) Test(org.junit.Test)

Example 5 with ImageConfig

use of com.imagepicker.media.ImageConfig in project react-native-image-picker by marcshilling.

the class ImageConfigTest method testParsingOptions.

@Test
public void testParsingOptions() {
    WritableMap options = defaultOptions();
    ImageConfig config = new ImageConfig(null, null, 0, 0, 0, 0, false);
    config = config.updateFromOptions(options);
    assertEquals("maxWidth", 1000, config.maxWidth);
    assertEquals("maxHeight", 600, config.maxHeight);
    assertEquals("quality", 50, config.quality);
    assertEquals("rotation", 135, config.rotation);
    assertTrue("storageOptions.cameraRoll", config.saveToCameraRoll);
}
Also used : ImageConfig(com.imagepicker.media.ImageConfig) WritableMap(com.facebook.react.bridge.WritableMap) Test(org.junit.Test)

Aggregations

ImageConfig (com.imagepicker.media.ImageConfig)7 File (java.io.File)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 Activity (android.app.Activity)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Matrix (android.graphics.Matrix)1 ExifInterface (android.media.ExifInterface)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 AlertDialog (android.support.v7.app.AlertDialog)1 ReactActivity (com.facebook.react.ReactActivity)1 ReactMethod (com.facebook.react.bridge.ReactMethod)1 WritableMap (com.facebook.react.bridge.WritableMap)1 UI (com.imagepicker.utils.UI)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1