0

As in the below code, I am capturing Images, cropping it and using them. On cropping the images, the image is badly distorted and having no quality.Please help me.

public class ScanActivity extends Activity{

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    captureImage();

}

 private void captureImage() {
    try {
        //Capture the image from rear Camera
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        //Crop the captured image
        cameraIntent.putExtra("crop", "true");
        cameraIntent.putExtra("aspectX", 0);
        cameraIntent.putExtra("aspectY", 0);
        cameraIntent.putExtra("outputX", 200);
        cameraIntent.putExtra("outputY", 200);
        startActivityForResult(cameraIntent, CAMERA_CAPTURE);

    } catch (Exception e) {
       Toast.makeText(ScanActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
    }

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != RESULT_OK || requestCode != CAMERA_CAPTURE)
        return;

    if (requestCode == CAMERA_CAPTURE) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            if (data != null) {
                Bitmap bitmap = extras.getParcelable("data");
                imageView.setImageBitmap(bitmap);

                // convert bitmap to JPEG to save as a byte[]
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // compress the image
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                // convert to byte array
                final byte[] imageInByte = stream.toByteArray();

}

I tried the below code but it doesnt seem to be of any help:

 int newWidth=200;
    int newHeight=200;
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

    imageView.setImageBitmap(scaledBitmap);
Kittu
  • 61
  • 9
  • There is no requirement for activities implementing `ACTION_IMAGE_CAPTURE` to honor those extras or otherwise provide cropping options. There are many [image cropping libraries for Android](https://android-arsenal.com/tag/45). Please use one. – CommonsWare Aug 11 '15 at 16:31
  • Thank you @CommonsWare. I was trying to use a cropping tool and I was able to implement it as a separate module.But, when integrated with my application, I am running into error. Can you please help me. Here is the post. http://stackoverflow.com/questions/32815141/outofmemoryerror-android-image-crop-issue – Kittu Sep 28 '15 at 03:19

0 Answers0