2013-05-22 40 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); 
    }