【问题标题】:Scaling down photo taken by camera and getting the picture Path缩小相机拍摄的照片并获取图片路径
【发布时间】:2012-09-10 12:10:53
【问题描述】:

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

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

谢谢

【问题讨论】:

标签: android android-camera android-image


【解决方案1】:

获取位图图像后,您可以使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多