如何在Android相机中设置拍照图片的尺寸和质量?

3

我写了一段代码,用相机拍照并将其转换为jpeg文件,然后将其作为ParseFile上传到Parse.com。

问题在于照片非常小(153 x 204像素),而且质量很低。

我需要照片至少为5 MP的质量,并将其裁剪成300x300像素。

这是我现在使用的代码。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_picture);

        img = (ImageView) findViewById(R.id.imgV);

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        byte[] image_byte_array;
              ParseObject post_object = new ParseObject("Gallery");
            Bundle extras = data.getExtras();
            Bitmap image = (Bitmap) extras.get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            image_byte_array = stream.toByteArray();
            ParseFile picture_file = new ParseFile("Picture.jpg", image_byte_array);
            picture_file.saveInBackground();
            post_object.put("photo", picture_file);
            post_object.saveInBackground();
        }

感谢您的提前帮助。
2个回答

2

请查看下面的链接

http://developer.android.com/guide/topics/media/camera.html#intent-image

您需要添加一个Uri,这是用于存储照片的文件的Uri。

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

一个完整的示例
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_picture);

    img = (ImageView) findViewById(R.id.imgV);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

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

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

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

    return mediaFile;
}

我不想将图片保存到手机存储中,我只需要使用我展示的代码直接上传到Parse.com服务器,如果您有任何想法,我会很感激知道我做错了什么。 - drilonrecica
通过使用相机意图,您无法避免将照片保存到存储中。如果您真的想跳过保存到存储器的步骤,您必须实现自己的相机。 - Derek Fung
onActivityResult返回的data只是一个缩略图,所以它很小。 - Derek Fung
好的,谢谢。@Darpan提供的答案对我帮助很大,还是谢谢;) - drilonrecica

2

请查看这里的答案。

默认情况下,相机意图会返回缩略图,因此需要获取完整的图像。

Derek的解决方案是正确的,但你需要改进它。

真正的重点在于获取完整的图像,具体实现如下:

thumbnail = MediaStore.Images.Media.getBitmap(
                            getContentResolver(), imageUri);
                    imgView.setImageBitmap(thumbnail);
                    imageurl = getRealPathFromURI(imageUri);    

完整路径将由getRealPathFromURI()给出。


谢谢,我会选择您的答案作为正确答案,因为您提供的链接帮助了我,感谢 ;) - drilonrecica
一个小提示 - 尽量多谷歌一下。在安卓开发中,除非是非常新的API,所有问题都已经得到了回答。这为我们提供了如此强大的支持基础 :) - Darpan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接