2011-04-15 40 views
0

我的應用程序訪問相機並捕獲圖片,然後將其保存回sdcard
問題是圖像在1500X2500像素和500kb大小的情況下相當大。
我必須將圖像上傳到服務器。
所以我想編輯圖像,但我想編輯在正確的構圖的圖像,即正確的高度:寬度比例,使圖像不會看起來很奇怪如何從SD卡正確編輯圖像並將其保存回

的代碼我使用的:

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); 

使用上面的代碼,我可以將圖像大小調整爲350 X 350,但是我希望僅將寬度作爲參數進行縮放,以便相應地調整高度,並且圖像比率保持不變。

另外還提及是否應該正在沖洗FileoutputsteaminputStream

回答

1

如何縮放圖像保持其縱橫比:

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

new_height = 500; 
new_width = orig_width * aspect;