abhishek
abhishek

Reputation: 1434

How to edit the image correctly from sd card and save it back

My application accesses the camera and captures the picture then saves it back in sdcard.
The problem is that image is quite big around 1500X2500 px and 500kb in size.
I have to upload the image to server.
So I thought of editing the image but I want to edit the image in correct composition i.e. correct height:width ratio so that image doesn't looks odd

The code which I am using:

FileInputStream in = new FileInputStream("/mnt/sdcard/DCIM/1302779969138");
BufferedInputStream buf = new BufferedInputStream(in)                                 
Bitmap bm = BitmapFactory.decodeStream(buf);
File isfile = new File(Environment.getExternalStorageDirectory(),"/DCIM/1302779969138");
FileOutputStream fout = new FileOutputStream(isfile);
Bitmap bitmap = null;

bitmap = Bitmap.createScaledBitmap(bm, 350, 350, true);
bitmap.compress(CompressFormat.JPEG, 20, fout);

With the above code I am able to resize the image to 350 X 350 but iI wish to scale it with only width as a parameter so that height adjusts accordingly and the image ratio remains the same

Plus also mention if I should be flushing Fileoutputsteam and inputStream

Upvotes: 0

Views: 1039

Answers (1)

Torp
Torp

Reputation: 7924

How to scale an image preserving its aspect ratio:

orig_width = bm.getWidth();
orig_height = bm.getHeight();
aspect = orig_width / orig_height;
new_width = 500;
new_height = orig_height / aspect;

or

new_height = 500;
new_width = orig_width * aspect;

Upvotes: 1

Related Questions