1

我正在尝试缩放用相机拍摄的照片,但我不确定在哪里或如何做到这一点。现在代码正在访问相机,拍照并在列表视图中显示它,我也想获取图片路径,但我也不确定如何执行此操作。任何帮助将不胜感激。

/**
     * This function is called when the add player picture button is clicked.
     * It accesses the devices gallery and the user can choose a picture
     * from the gallery.
     * Or if the user chooses to take a picture with the camera, it handles that
     */

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();
                this.picPath = selectedImage.getPath();
                Bitmap bitmap;
                try {
                     bitmap = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);
                    imageView = (ImageView) findViewById(R.id.imagePlayer); 
                    imageView.setImageBitmap(bitmap);
                    Toast.makeText(this, selectedImage.toString(),
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
            }

谢谢

4

1 回答 1

2

获取位图图像后,您可以使用 Bitmap 类中的 createScaledBitmap 静态方法

   Bitmap.createScaledBitmap(yourBitmap, 50, 50, true); // Width and Height in pixel e.g. 50

但在未来的极端内存不足的 情况...

所以为避免java.lang.OutOfMemory 异常,请在解码之前检查位图的尺寸,除非您绝对相信来源会为您提供可预测大小的图像数据,这些数据可以舒适地放入可用内存中。

  // below 3 line of code will come instead of 
//imageView.setImageBitmap(bitmap);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();         
    photo.compress(Bitmap.CompressFormat.JPEG,100,stream);
    imageView.setImageBitmap(decodeSampledBitmapFromByte(stream.toByteArray(),50,50));  

BitmapFactory 类提供了几种解码方法(decodeByteArray()、decodeFile()、decodeResource() 等),用于从各种来源创建 Bitmap。根据您的图像数据源选择最合适的解码方法。这些方法尝试为构造的位图分配内存,因此很容易导致 OutOfMemory 异常。每种类型的解码方法都有额外的签名,允许您通过 BitmapFactory.Options 类指定解码选项。解码时将 inJustDecodeBounds 属性设置为 true 可避免内存分配,为位图对象返回 null,但设置 outWidth、outHeight 和 outMimeType。此技术允许您在构建位图(和内存分配)之前读取图像数据的尺寸和类型。

 // please define following two methods in your activity
    public Bitmap decodeSampledBitmapFromByte(byte[] res,
                int reqWidth, int reqHeight) {

            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(res, 0, res.length,options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeByteArray(res, 0, res.length,options);
        }
         public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float)height / (float)reqHeight);
                } else {
                    inSampleSize = Math.round((float)width / (float)reqWidth);
                }
            }
            return inSampleSize;
        }

And please refer following link from Android Training any bitmap related java.lang.OutofMemoryError: bitmap size exceeds VM budget http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

于 2012-09-10T14:33:24.233 回答