breezertwo
breezertwo

Reputation: 585

Change Picture Size/Resolution of IMAGE_CAPTURE Intent

Is it possible to change the settings of an Intent for taking a picture to set the size or resolution of the resulting Image?

So right now I take a picture and the resulting Image was taken with 16MP and 4608x2304.

I'd like to know if its possible to get a resulting image such as (for example): 2MP and 460x230...

I know there is this way:

intent.putExtra("outputX", 460);
intent.putExtra("outputY", 230);

But I'm looking for something that works with all devices (cause of course they all don't have the same image sizes and if I crop them with hard coded values it'll suck)...

Hope you can understand what my issue is..

Upvotes: 0

Views: 2173

Answers (1)

Oleg Sokolov
Oleg Sokolov

Reputation: 1143

You need to take a photo without saving it in a file:

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

After that you need to scale a result Bitmap with saving of aspect ratio:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        Bitmap scaledImage = scaleBitmap(imageBitmap , 460, 230);
        mImageView.setImageBitmap(scaledImage);
    }
}

private Bitmap scaleBitmap(Bitmap bm, int maxWidth, int maxHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();

    if (width > height) {
        // landscape
        int ratio = width / maxWidth;
        width = maxWidth;
        height = height / ratio;
    } else if (height > width) {
        // portrait
        int ratio = height / maxHeight;
        height = maxHeight;
        width = width / ratio;
    } else {
        // square
        height = maxHeight;
        width = maxWidth;
    }

    bm = Bitmap.createScaledBitmap(bm, width, height, true);
    return bm;
}

Upvotes: 2

Related Questions