how to save captured image with custom size in android

3.4k views Asked by At

in my application i can open the camera and take a picture. The picture is stored in a full size of 2448x3264 pixels on the sd-card. how can i configure this in my application, to save the picture in a size of 90x90 pixels and not in 2448x3264 pixel?

to open the camera and capture a image i use following methods:

/*
 * Capturing Camera Image will lauch camera app requrest image capture
 */
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

private Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

private File getOutputMediaFile(int type) {
    // External sdcard location
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory
            (Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } 
    else {
        return null;
    }

    return mediaFile;
}

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

/*              
                try {
                    decodeUri(this, fileUri, 90, 90);
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                }
*/

                // successfully captured the image
                Toast.makeText(getApplicationContext(), 
                        "Picture successfully captured", Toast.LENGTH_SHORT).show();
            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(), 
                        "User cancelled image capture", Toast.LENGTH_SHORT).show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
            }
        } 
    }   

public static Bitmap decodeUri(Context c, Uri uri, final int requiredWidth, final int requiredHeight) throws FileNotFoundException {

        BitmapFactory.Options o = new BitmapFactory.Options();

        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;

        while(true) {
            if(width_tmp / 2 < requiredWidth || height_tmp / 2 < requiredHeight)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
    }  

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

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }

i hope that s.o. can help me with this. i am trying to load the captured images into a little imageview, look like that. thanks in advance

4

There are 4 answers

1
Hamid Shatu On

Here, I'm giving a method which will take the saved path on SDCard of taken picture and will return the required size image as Bitmap. Now what you have to do is just pass image path on SDCard and get the resized image.

private Bitmap processTakenPicture(String fullPath) {

    int targetW = 90; //your required width
    int targetH = 90; //your required height

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(fullPath, bmOptions);

    int scaleFactor = 1;
    scaleFactor = calculateInSampleSize(bmOptions, targetW, targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor * 2;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(fullPath, bmOptions);

    return bitmap;
}

private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,
        int reqHeight) {

    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}
1
Mina Tadros On

After you had read the original image, you can use:

 Bitmap.createScaledBitmap(photo, width, height, true);
0
Alex Cohn On

No, you cannot control the picture size when you use MediaStore.ACTION_IMAGE_CAPTURE Intent. You can achieve this if you implement your "custom camera" (and there are plenty of working samples on Internet), including mine.

The byte array received in onPictureTaken() is a Jpeg buffer. Look at this Java package for image manipulation: http://mediachest.sourceforge.net/mediautil/ (there is an Android port on GitHub). There are very powerful and efficient methods to scale down Jpeg, without decoding it into Bitmap and back.

0
sirFunkenstine On

here is another question wherre a guy has the same problem. He uses the following.

    Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);