2013-11-01 64 views
0

在我的android應用程序中,當我從相機捕獲圖像時,我想重新調整它的尺寸。當我從畫廊拍攝圖像時,我很成功地重新調整大小。但是當我從相機捕捉時,我失敗了。請幫助從Android相機捕獲圖像時獲得更小尺寸的圖像

if (requestCode == take_image && resultCode == RESULT_OK && null != data) { 
    thumbnail = (Bitmap) data.getExtras().get("data"); 
    image2 = me1; 
    addattachmentsToListView(image2); 
} 

這裏是從SD卡調整圖像的代碼:

if (requestCode == UploadFile && resultCode == RESULT_OK && null != data) { 
    Uri selectedImage = data.getData(); 
    try { 

     Bitmap image=(decodeUri(selectedImage)); 
     addattachmentsToListView(image); 
     } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 


private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException { 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(
    getContentResolver().openInputStream(selectedImage), null, o); 

    final int REQUIRED_SIZE = 200; 

    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp/1.5 < REQUIRED_SIZE || height_tmp/1.5 < REQUIRED_SIZE) { 
      break; 
     } 
     width_tmp /= 4; 
     height_tmp /= 4; 

     // width_tmp = 20; 
     // height_tmp = 20; 
     scale *= 2; 
    } 

    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    return BitmapFactory.decodeStream(
    getContentResolver().openInputStream(selectedImage), null, o2); 
} 
+0

您是將圖像保存在設備上還是想調整從相機獲取的框架? – fasteque

+0

@fasteque我想調整框架 – user2768215

+0

檢查此http://stackoverflow.com/questions/20799901/creating-dcf-thumbnail-from-jpeg你在bmp中縮放圖像並再次創建scaled JPG。 –

回答

0

在你public void onPreviewFrame(byte[] data, Camera camera),你可以通過這種方式獲得的位圖:

Bitmap bmp = YUVtoBMP(data, ImageFormat.NV21, YOUR_CAMERA_SIZE); 

public Bitmap YUVtoBMP(byte[] data, int format, Camera.Size size) 
{ 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    YuvImage yuvImage = new YuvImage(data, format, size.width, size.height, null); 
    yuvImage.compressToJpeg(new Rect(0, 0, size.width, size.height), 50, out); 
    byte[] imageBytes = out.toByteArray(); 
    Options options = new Options(); 
    options.inPreferQualityOverSpeed = false; 
    Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options); 

    return image; 
} 

爲了擴大一個位圖,你可以:

Bitmap scaled = Bitmap.createScaledBitmap(bmp, NEW_WIDTH, NEW_HEIGHT, true); 

這只是一個提示:從此開始並嘗試尋找優化(如果有的話)。