【问题标题】:How to set the size and quality of a picture taken with the android camera?如何设置用安卓相机拍摄的照片的大小和质量?
【发布时间】:2015-11-30 21:34:14
【问题描述】:

我编写了这段代码,它用相机拍照,然后将其转换为 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();
        }

提前致谢。

【问题讨论】:

    标签: android image android-camera crop image-quality


    【解决方案1】:

    查看this answer here.

    相机意图默认返回缩略图,因此您需要 获取完整图像。

    Derek 的解决方案是正确的,但您必须改进它。

    真正的作用在于获取此处发生的完整图像 -

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

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

    【讨论】:

    • 谢谢你,我会选择你的答案作为正确的答案,因为你链接的答案对我有帮助,谢谢;)
    • 小贴士 - 多用谷歌搜索一下。在android中,除非是很新的API;所有的问题都已经回答了。对我们来说是一个很好的支持基础:)
    【解决方案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 服务器,所以如果你有任何想法我很高兴知道我做错了什么。
    • 通过使用Camera Intent,您无法避免将图片保存到存储中。如果你真的想跳过保存到存储,你必须实现你自己的相机。
    • onActivityResult 中返回的data 只是一个缩略图,所以它很小。
    • 好的,谢谢,@Darpan 链接的答案对我帮助很大,仍然感谢 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多