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 x 320的<100kb大小的图像。

4

3 回答 3

2

你可以看看我写的这篇关于如何从设备的相机活动中获取图像的博客文章:

指南:Android:将相机活动用于缩略图和全尺寸图像

如果您查看指南的末尾,您有以下方法:

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

您可以在那里提供图像输出所需的尺寸。

于 2013-05-22T07:22:00.017 回答
1

你必须使用inSampleSizefrom 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;
}
于 2013-05-22T07:11:24.407 回答
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);
  }
于 2013-05-22T07:44:11.333 回答