private void saveOutput(Bitmap croppedImage) {
    if (saveUri != null) {
      OutputStream outputStream = null;
      try {
        outputStream = getContentResolver().openOutputStream(saveUri);
        if (outputStream != null) {
          croppedImage.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
        }
      } catch (IOException e) {
        setResultException(e);
        Log.e("Cannot open file: " + saveUri, e);
      } finally {
        CropUtil.closeSilently(outputStream);
      }

      CropUtil.copyExifRotation(
          CropUtil.getFromMediaUri(this, getContentResolver(), sourceUri),
          CropUtil.getFromMediaUri(this, getContentResolver(), saveUri));

      setResultUri(saveUri);
    }

    final Bitmap b = croppedImage;
    handler.post(
        new Runnable() {
          public void run() {
            imageView.clear();
            b.recycle();
          }
        });

    finish();
  }
  private void loadInput() {
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();

    if (extras != null) {
      aspectX = extras.getInt(Crop.Extra.ASPECT_X);
      aspectY = extras.getInt(Crop.Extra.ASPECT_Y);
      maxX = extras.getInt(Crop.Extra.MAX_X);
      maxY = extras.getInt(Crop.Extra.MAX_Y);
      saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
    }

    sourceUri = intent.getData();
    if (sourceUri != null) {
      exifRotation =
          CropUtil.getExifRotation(CropUtil.getFromMediaUri(this, getContentResolver(), sourceUri));

      InputStream is = null;
      try {
        sampleSize = calculateBitmapSampleSize(sourceUri);
        is = getContentResolver().openInputStream(sourceUri);
        BitmapFactory.Options option = new BitmapFactory.Options();
        option.inSampleSize = sampleSize;
        rotateBitmap = new RotateBitmap(BitmapFactory.decodeStream(is, null, option), exifRotation);
      } catch (IOException e) {
        Log.e("Error reading image: " + e.getMessage(), e);
        setResultException(e);
      } catch (OutOfMemoryError e) {
        Log.e("OOM reading image: " + e.getMessage(), e);
        setResultException(e);
      } finally {
        CropUtil.closeSilently(is);
      }
    }
  }
Esempio n. 3
0
  private void startCrop() {
    if (isFinishing()) {
      return;
    }
    imageView.setImageRotateBitmapResetBase(rotateBitmap, true);
    CropUtil.startBackgroundJob(
        this,
        null,
        getResources().getString(R.string.crop__wait),
        new Runnable() {

          public void run() {
            final CountDownLatch latch = new CountDownLatch(1);
            handler.post(
                new Runnable() {

                  public void run() {
                    if (imageView.getScale() == 1F) {
                      imageView.center(true, true);
                    }
                    latch.countDown();
                  }
                });
            try {
              latch.await();
            } catch (InterruptedException e) {
              throw new RuntimeException(e);
            }
            new Cropper().crop();
          }
        },
        handler);
  }
Esempio n. 4
0
  @TargetApi(10)
  private Bitmap decodeRegionCrop(Bitmap croppedImage, Rect rect) {
    // Release memory now
    clearImageView();

    InputStream is = null;
    try {
      is = getContentResolver().openInputStream(sourceUri);
      BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
      final int width = decoder.getWidth();
      final int height = decoder.getHeight();

      if (exifRotation != 0) {
        // Adjust crop area to account for image rotation
        Matrix matrix = new Matrix();
        matrix.setRotate(-exifRotation);

        RectF adjusted = new RectF();
        matrix.mapRect(adjusted, new RectF(rect));

        // Adjust to account for origin at 0,0
        adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
        rect =
            new Rect(
                (int) adjusted.left,
                (int) adjusted.top,
                (int) adjusted.right,
                (int) adjusted.bottom);
      }

      try {
        croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());

      } catch (IllegalArgumentException e) {
        // Rethrow with some extra information
        throw new IllegalArgumentException(
            "Rectangle "
                + rect
                + " is outside of the image ("
                + width
                + ","
                + height
                + ","
                + exifRotation
                + ")",
            e);
      }

    } catch (IOException e) {
      Log.e("Error cropping image: " + e.getMessage(), e);
      finish();
    } catch (OutOfMemoryError e) {
      Log.e("OOM cropping image: " + e.getMessage(), e);
      setResultException(e);
    } finally {
      CropUtil.closeSilently(is);
    }
    return croppedImage;
  }
 private void saveImage(Bitmap croppedImage) {
   if (croppedImage != null) {
     final Bitmap b = croppedImage;
     CropUtil.startBackgroundJob(
         this,
         null,
         getResources().getString(R.string.crop__saving),
         new Runnable() {
           public void run() {
             saveOutput(b);
           }
         },
         handler);
   } else {
     finish();
   }
 }
Esempio n. 6
0
  private int calculateBitmapSampleSize(Uri bitmapUri) throws IOException {
    InputStream is = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    try {
      is = getContentResolver().openInputStream(bitmapUri);
      BitmapFactory.decodeStream(is, null, options); // Just get image size
    } finally {
      CropUtil.closeSilently(is);
    }

    int maxSize = getMaxImageSize();
    int sampleSize = 1;
    while (options.outHeight / sampleSize > maxSize || options.outWidth / sampleSize > maxSize) {
      sampleSize = sampleSize << 1;
    }
    return sampleSize;
  }
Esempio n. 7
0
  private void saveOutput(Bitmap croppedImage) {
    if (saveUri != null) {
      OutputStream outputStream = null;
      try {
        outputStream = getContentResolver().openOutputStream(saveUri);
        if (outputStream != null) {

          if (exifRotation > 0) {
            try {
              Matrix matrix = new Matrix();
              matrix.reset();
              matrix.postRotate(exifRotation);
              Bitmap bMapRotate =
                  Bitmap.createBitmap(
                      croppedImage,
                      0,
                      0,
                      croppedImage.getWidth(),
                      croppedImage.getHeight(),
                      matrix,
                      true);
              bMapRotate.compress(Bitmap.CompressFormat.PNG, 70, outputStream);

            } catch (Exception e) {
              e.printStackTrace();
              croppedImage.compress(Bitmap.CompressFormat.PNG, 70, outputStream);
            } finally {
              if (croppedImage != null && !croppedImage.isRecycled()) {
                croppedImage.recycle();
              }
            }
          } else {
            croppedImage.compress(Bitmap.CompressFormat.PNG, 70, outputStream);
          }
        }

      } catch (IOException e) {
        setResultException(e);
        Log.e("Cannot open file: " + saveUri, e);
      } finally {
        CropUtil.closeSilently(outputStream);
      }

      if (!IN_MEMORY_CROP) {
        // In-memory crop negates the rotation
        CropUtil.copyExifRotation(
            CropUtil.getFromMediaUri(getContentResolver(), sourceUri),
            CropUtil.getFromMediaUri(getContentResolver(), saveUri));
      }

      setResultUri(saveUri);
    }

    final Bitmap b = croppedImage;
    handler.post(
        new Runnable() {

          public void run() {
            imageView.clear();
            b.recycle();
          }
        });

    finish();
  }