2

在我的应用程序中,我可以打开相机并拍照。图片以 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");
    }

我希望这可以帮助我。我正在尝试将捕获的图像加载到一个小图像视图中,看起来像那样。提前致谢

4

4 回答 4

1

MediaStore.ACTION_IMAGE_CAPTURE不可以,使用Intent时无法控制图片大小。如果你实现你的“自定义相机”(互联网上有很多工作示例),你可以实现这一点,包括的。

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

于 2014-02-12T13:39:15.673 回答
0

在这里,我给出了一个方法,它将在 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;
}
于 2014-02-11T11:42:48.867 回答
0

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

    Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
于 2014-02-12T10:34:46.267 回答
0

阅读原始图像后,您可以使用:

 Bitmap.createScaledBitmap(photo, width, height, true);
于 2014-02-11T11:44:37.850 回答