如何从原生相机获取特定尺寸的图像

时间:2013-05-22 06:54:19

标签: android camera

这是我用来获取完整尺寸图片的代码,尺寸为> 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);
}

有没有办法获取特定尺寸的图像,在我的情况下是< 100kb 尺寸的尺寸 480 x 320 的图像。

3 个答案:

答案 0 :(得分: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 :(得分:1)

您必须使用inSampleSize中的BitmapFactory.Options。通过它,解码器将对inSampleSize的图像进行下采样。例如,inSampleSize = 2会创建width/2heigth/2

的位图
int destWidth = 480;
int destHeight = 320;


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

答案 2 :(得分: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);
  }