【问题标题】:how to save captured image with custom size in android如何在android中保存自定义大小的捕获图像
【发布时间】:2014-03-09 04:05:01
【问题描述】:

在我的应用程序中,我可以打开相机并拍照。图片以 2448x3264 像素的全尺寸存储在 sd 卡上。如何在我的应用程序中配置它,以将图片保存为 90x90 像素而不是 2448x3264 像素?

要打开相机并拍摄图像,我使用以下方法:

/*
 * Capturing Camera Image will lauch camera app requrest image capture
 */
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

private Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

private File getOutputMediaFile(int type) {
    // External sdcard location
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory
            (Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } 
    else {
        return null;
    }

    return mediaFile;
}

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

/*              
                try {
                    decodeUri(this, fileUri, 90, 90);
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                }
*/

                // successfully captured the image
                Toast.makeText(getApplicationContext(), 
                        "Picture successfully captured", Toast.LENGTH_SHORT).show();
            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(), 
                        "User cancelled image capture", Toast.LENGTH_SHORT).show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
            }
        } 
    }   

public static Bitmap decodeUri(Context c, Uri uri, final int requiredWidth, final int requiredHeight) throws FileNotFoundException {

        BitmapFactory.Options o = new BitmapFactory.Options();

        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;

        while(true) {
            if(width_tmp / 2 < requiredWidth || height_tmp / 2 < requiredHeight)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
    }  

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }

我希望如此。可以帮我解决这个问题。我正在尝试将捕获的图像加载到一个小图像视图中,看起来像that。提前谢谢

【问题讨论】:

  • 位图缩放 = Bitmap.createScaledBitmap(largeBitmap, height, width, true);
  • 弗兰肯斯汀先生,感谢您的帮助。请看下面我的回答
  • 没有人能帮我解决这个问题吗?我只希望它是捕获图像的保存缩略图,以便在自定义列表视图中使用它。
  • 抓拍初始图片后不能创建ScaledBitmap吗?
  • 不幸的是没有。因为我不知道在哪里以及如何将其保存为位图。 SD卡上保存的图像为.jpg格式。是否可以在将其保存到 sd 卡之前将其调整为缩略图格式?

标签: android android-listview android-camera android-bitmap android-capture


【解决方案1】:

不,使用MediaStore.ACTION_IMAGE_CAPTURE Intent 时无法控制图片大小。如果你实现你的"custom camera"(并且互联网上有很多工作示例),你可以实现这一点,包括mine

onPictureTaken() 中接收到的字节数组是一个 Jpeg 缓冲区。查看这个用于图像处理的 Java 包:http://mediachest.sourceforge.net/mediautil/(有一个 Android 端口on GitHub)。有非常强大和有效的方法来缩小 Jpeg,无需将其解码为位图并返回。

【讨论】:

    【解决方案2】:

    在这里,我给出了一个方法,它将在 SDCard 上保存已拍摄照片的路径,并将所需大小的图像作为位图返回。现在您要做的就是在 SDCard 上传递图像路径并获取调整大小的图像。

    private Bitmap processTakenPicture(String fullPath) {
    
        int targetW = 90; //your required width
        int targetH = 90; //your required height
    
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(fullPath, bmOptions);
    
        int scaleFactor = 1;
        scaleFactor = calculateInSampleSize(bmOptions, targetW, targetH);
    
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor * 2;
        bmOptions.inPurgeable = true;
    
        Bitmap bitmap = BitmapFactory.decodeFile(fullPath, bmOptions);
    
        return bitmap;
    }
    
    private 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;
    }
    

    【讨论】:

    • 谢谢哈米德,但我想在开始时将其保存为自定义大小。这就是我想要的。有没有办法做到这一点?我正在使用简单光标适配器在自定义列表视图中加载信息。这意味着,我必须提供资源 ID(例如:R.id.image)
    【解决方案3】:

    看完原图后,可以使用:

     Bitmap.createScaledBitmap(photo, width, height, true);
    

    【讨论】:

    • 感谢你的帮助。我想将其保存为自定义大小。见我上面的回答。
    【解决方案4】:

    这里是另一个question 哪里有一个人有同样的问题。他使用以下内容。

        Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
    

    【讨论】:

      猜你喜欢
      • 2011-08-12
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多