1

I am taking photo using in-built Camera App, and getting resolution of image (1600 x 1200) but I would like to save my all images in (1200 x 900) into SD Card for that I have written a method but still getting images in original size.

Here is my code, which I am using to capture and store images into SD Card

public class FormActivity extends AppCompatActivity {

String filePath = null;
File file;
Uri output;
final int requestCode = 100;

String stringImageName= null;

static int w = 1200;
static int h = 900;

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

    setContentView(R.layout.activity_form);     

    SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
    stringImageName = s.format(new Date());
    Log.d("format::", stringImageName);

    filePath = Environment.getExternalStorageDirectory() + "/"+stringImageName+".jpeg";
    file = new File(filePath);
    output = Uri.fromFile(file);


    buttonOrderNow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            photoCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);                       
            startActivityForResult(photoCaptureIntent, requestCode);

        }
    });

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(this.requestCode == requestCode && resultCode == RESULT_OK) {

         resize();

    }
}

private void resize() {

    Long startTime = System.currentTimeMillis();

    Bitmap bitmap_Source = BitmapFactory.decodeFile(filePath);
     float factorH = h / (float)bitmap_Source.getHeight();
     float factorW = w / (float)bitmap_Source.getWidth();
     float factorToUse = (factorH > factorW) ? factorW : factorH;
     Bitmap bm = Bitmap.createScaledBitmap(bitmap_Source, 
       (int) (bitmap_Source.getWidth() * factorToUse), 
       (int) (bitmap_Source.getHeight() * factorToUse), 
       false);      

     Long endTime = System.currentTimeMillis();
     Long processTime = endTime - startTime;
     Toast.makeText(FormActivity.this, ""+processTime, Toast.LENGTH_LONG).show();

    }
Sun
  • 6,768
  • 25
  • 76
  • 131
  • Probably, the question was downvoted because you show no attempt to resolve your problem. Even if you look at the list of "related" questions on the right, you see [Resize image taken from gallery or camera, before being uploaded](http://stackoverflow.com/questions/23813604/resize-image-taken-from-gallery-or-camera-before-being-uploaded?rq=1) which provides a viable answer. – Alex Cohn Nov 18 '15 at 09:38
  • @AlexCohn check my updated code, I have given my try, but still issue not resolved – Sun Nov 18 '15 at 09:39
  • Cropping do resize your images into small KB's http://stackoverflow.com/questions/29532914/android-intent-with-multiple-option-i-e-pick-image-from-gallary-and-capture-im/29560548#29560548 – Anoop M Maddasseri Nov 18 '15 at 10:35
  • What is missing now? That the scaled image is not written back to file? – Alex Cohn Nov 18 '15 at 12:16
  • BTW, make sure that you never scale the image up! – Alex Cohn Nov 18 '15 at 12:17

2 Answers2

0

I guess this, u should pass the value height and width Pass your Image path and get exact image

private Bitmap getBitmap(String path) {

Uri uri = getImageUri(path);
InputStream in = null;
try {
    final int IMAGE_MAX_SIZE = 1920000; // 1.9MP
    in = mContentResolver.openInputStream(uri);
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(in, null, o);
    in.close();
    int scale = 1;
    while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > 
          IMAGE_MAX_SIZE) {
       scale++;
    }
    Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", 
       orig-height: " + o.outHeight);
    Bitmap b = null;
    in = mContentResolver.openInputStream(uri);
    if (scale > 1) {
        scale--;
        // scale to max possible inSampleSize that still yields an image
        // larger than target
        o = new BitmapFactory.Options();
        o.inSampleSize = scale;
        b = BitmapFactory.decodeStream(in, null, o);

        // resize to desired dimensions
        int height = b.getHeight(); 
        int width = b.getWidth();
        Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
           height: " + height);

        double y = Math.sqrt(IMAGE_MAX_SIZE
                / (((double) width) / height));
        double x = (y / height) * width;

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
           (int) y, true);
        b.recycle();
        b = scaledBitmap;

        System.gc();
    } else {
        b = BitmapFactory.decodeStream(in);
    }
    in.close();

    Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + 
       b.getHeight());
    return b;
} catch (IOException e) {
    Log.e(TAG, e.getMessage(),e);
    return null;
}
User Learning
  • 3,165
  • 5
  • 30
  • 51
-1

Taken from here .

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if(this.requestCode == requestCode && resultCode == RESULT_OK){  
        Bitmap yourBitmap= (Bitmap) data.getExtras().get("data"); 
        //Resize it as you need
        Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, 1200, 900, true);

        //Now you can save it
    }  
} 
Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • -1 because the OP wants to resize the image before he saves it. In `onActivityResult()` the picture is already saved. Additionally, your code loads a thumbnail, not the original size, so this is wrong too. See: https://developer.android.com/training/camera/photobasics.html#TaskPhotoView – Bevor Apr 07 '18 at 10:13
  • @Bevor Its wrong that " In onActivityResult() the picture is already saved" .. `onActivityResult` is the only step where you will get the captured pic/bitmaps in original size..you can assume like its (onActivityResult) the next step of clicking capture button in device camera..then you can do whatever you want. my answer is pretty straight forward and perfect. it simply resizing the bitmaps collected from camera with a proper checking of request and result code. – Ranjit Apr 10 '18 at 06:50