缩小照相机拍摄的照片并获取照片路径

时间:2012-09-10 12:10:53

标签: android android-camera android-image

我正在尝试缩放用相机拍摄的照片,但我不知道在哪里或如何做到这一点。现在代码正在访问相机,拍照并在列表视图中显示它,我也想获得图片路径,但我不确定如何做到这一点。任何帮助都将受到高度赞赏。

/**
     * 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());
                }
            }

由于

1 个答案:

答案 0 :(得分:2)

获取位图图像后,您可以使用Bitmap类

中的createScaledBitmap静态方法
   Bitmap.createScaledBitmap(yourBitmap, 50, 50, true); // Width and Height in pixel e.g. 50

但未来极端记忆力低下......  如果你不小心,位图可以快速消耗你的可用内存预算,导致应用程序崩溃,因为可怕的异常: java.lang.OutofMemoryError:位图大小超过VM预算

所以为了避免 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()等),用于从各种源创建位图。根据图像数据源选择最合适的解码方法。这些方法尝试为构造的位图分配内存,因此很容易导致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;
        }

请参阅Android培训中的以下链接相关的任何位图 java.lang.OutofMemoryError:位图大小超过VM预算 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html