2013-05-22 43 views
1

這是我用來獲取全尺寸圖像的代碼,尺寸爲> 2mb尺寸,3560 X 1876尺寸。如何從本機攝像頭獲取特定尺寸的圖像

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    image = (ImageView) findViewById(R.id.image); 
    button = (Button) findViewById(R.id.button); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

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

      Intent i = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      i.putExtra(MediaStore.EXTRA_OUTPUT, output); 
      startActivityForResult(i, CAPTURE_IMAGE); 
     } 
    }); 
} 

    @Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    Bitmap photo = BitmapFactory.decodeFile(filePath, options); 
    image.setImageBitmap(photo); 
} 

有沒有辦法讓特定大小的圖像,在我的情況的尺寸480×320< 100KB大小的圖像。

+0

檢查這篇文章,幫助你。 http://stackoverflow.com/questions/8757341/android-reduce-size-of-camera-picture 感謝 –

+0

@MdAbdulGafur是啊,我看到了帖子,但它使用專用相機。 –

回答

2

你可以看看這篇博客文章中,我就寫了如何從設備的攝像頭活動圖片:

Guide: Android: Use Camera Activity for Thumbnail and Full Size Image

如果您看看指南的結尾,你有這種方法:

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ // BEST QUALITY MATCH 

//First decode with inJustDecodeBounds=true to check dimensions 
final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(path, options); 

// Calculate inSampleSize, Raw height and width of image 
final int height = options.outHeight; 
final int width = options.outWidth; 
options.inPreferredConfig = Bitmap.Config.RGB_565; 
int inSampleSize = 1; 

if (height > reqHeight) 
{ 
    inSampleSize = Math.round((float)height/(float)reqHeight); 
} 
int expectedWidth = width/inSampleSize; 

if (expectedWidth > reqWidth) 
{ 
    //if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize.. 
    inSampleSize = Math.round((float)width/(float)reqWidth); 
} 

options.inSampleSize = inSampleSize; 

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 

return BitmapFactory.decodeFile(path, options); 
} 

喲你可以爲你的圖像輸出提供所需的尺寸。

1

您必須使用BitmapFactory.OptionsinSampleSize。通過它,解碼器會下載您的inSampleSize圖片。例如inSampleSize = 2將創建width/2位圖和heigth/2

int destWidth = 480; 
int destHeight = 320; 


while ((bitmapWidth/2 > destWidth) 
       || (bitmapHeight/2 > destHeight)) { 
     ++inSampleSize; 
     bitmapWidth /= 2; 
     bitmapHeight /=2; 
} 
+0

位圖已經保存到我用'i.putExtra(MediaStore.EXTRA_OUTPUT,output)給出的路徑;'所以,我應該在哪裏使用這個** inSampleSize **? –

+0

以及由你決定。例如,您可以每次需要在應用程序中使用該位圖,或者第一次解碼並將縮減採樣副本保存在特定路徑中 – Blackbelt

+0

因此,我應該獲取已保存的位圖,調整其大小並替換之前的位圖? –

1

如果使用最簡單的方法來做到這一點會怎麼樣。複製此方法,將您的位圖和所需的高度和寬度,得到縮放圖像回:)

public static Bitmap resizeBitmap(Bitmap originalBitmap, int width, int height) { 

    float scaleWidth = ((float) width)/originalBitmap.getWidth(); 
    float scaleHeight = ((float) height)/originalBitmap.getHeight(); 

    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 

    return Bitmap.createBitmap(originalBitmap, 0, 0, original.getWidth(),original.getHeight(), matrix, true); 
    }