【问题标题】:High quality image picked from gallery or captured from camera从图库中挑选或从相机捕获的高质量图像
【发布时间】:2013-12-14 04:47:47
【问题描述】:

我有 3 个任务:

  1. 从图库中选择图片
  2. 从相机捕捉图像
  3. 从 1 或 2 裁剪图像。

画质一定要好。

我的问题有什么解决方案吗?可能是一些图书馆?

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 你尝试过什么了吗?你有什么问题吗?
  • 问题太多了,我决定从像答案这样的明确建议开始。
  • 现在问题是使用这个代码stackoverflow.com/a/20261321/1012234我不能像正方形那样裁剪图像。我会告诉你:take.ms/6379o

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


【解决方案1】:

默认情况下,Android 实际上拥有您列举的所有内容。

  1. 从图库中选择图片

    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(pickPhoto, FROM_GALLERY);
    
  2. 从相机捕捉图像

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePicture, FROM_CAMERA);
    
  3. 获取上述意图的结果并进行裁剪

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case FROM_GALLERY:
        case FROM_CAMERA: {
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = data.getData();
    
                Intent cropIntent = new Intent("com.android.camera.action.CROP");
                cropIntent.setDataAndType(selectedImage, "image/*");
                cropIntent.putExtra("crop", "true");
                cropIntent.putExtra("aspectX", 1);
                cropIntent.putExtra("aspectY", 1);
                cropIntent.putExtra("outputX", MAX_WIDTH);
                cropIntent.putExtra("outputY", MAX_HEIGHT);
                cropIntent.putExtra("return-data", true);
                startActivityForResult(cropIntent, PICTURE_CROP);
            }
            break;
        }
        case PICTURE_CROP: {
            if (resultCode == Activity.RESULT_OK) {
                final Bundle extras = data.getExtras();
    
                if (extras != null) {
                    Bitmap photo = extras.getParcelable("data");
    
                    // Hurray! You now have the photo as a Bitmap
                }
            }
            break;
        }
        }
    }
    

更新:

根据post,您不应使用com.android.camera.action.CROP,因为并非所有设备都存在这种情况。在那篇文章中,他还列举了替代方案,我也会在这里列出:

  1. https://github.com/lvillani/android-cropimage
  2. https://github.com/biokys/cropimage

【讨论】:

  • 什么是 ParseManager?
  • 抱歉,这是从我的一个项目中复制而来的。我已编辑我的答案以删除项目特定代码。
  • 您的代码对我有用,但默认情况下我无法裁剪方形图像。看屏幕:take.ms/6379o
  • MAX_WIDTH 和 MAX_HEIGHT 使用什么值?您也可以尝试删除这些参数。
  • MAX_WIDTH = 1000; MAX_HEIGHT = 1000;是的,我可以删除它们,但我真的需要最大。大小为 1000。此外,当我删除它们时,我有无限加载对话框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多