【问题标题】:How to crop images from camera如何从相机中裁剪图像
【发布时间】:2014-12-09 00:32:49
【问题描述】:

如何裁剪相机图像。现在它正在显示要裁剪的图像,并在点击“保存”按钮的同时选择裁剪部分。它显示为“保存图片”。之后什么都没有发生。这是我的代码。

按钮点击:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, CAMERA_PIC_REQUEST);

onActivityResult :

Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
if (bitmap != null) {
    Img_View.setImageBitmap(bitmap);
}

【问题讨论】:

    标签: android image android-camera crop


    【解决方案1】:

    您可以使用此代码执行裁剪:

    .....
    final int CAMERA_CAPTURE = 1;
    final int CROP_PIC = 2;
    private Uri picUri;
    ....
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button captureBtn = (Button) findViewById(R.id.capture_btn);
            captureBtn.setOnClickListener(this);
        }
    
        public void onClick(View v) {
            if (v.getId() == R.id.capture_btn) {
                try {
                    // use standard intent to capture an image
                    Intent captureIntent = new Intent(
                            MediaStore.ACTION_IMAGE_CAPTURE);
                    // we will handle the returned data in onActivityResult
                    startActivityForResult(captureIntent, CAMERA_CAPTURE);
                } catch (ActivityNotFoundException anfe) {
                    Toast toast = Toast.makeText(this, "This device doesn't support the crop action!",
                            Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        }
    
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == CAMERA_CAPTURE) {
                    // get the Uri for the captured image
                    picUri = data.getData();
                    performCrop();
                }
                // user is returning from cropping the image
                else if (requestCode == CROP_PIC) {
                    // get the returned data
                    Bundle extras = data.getExtras();
                    // get the cropped bitmap
                    Bitmap thePic = extras.getParcelable("data");
                    ImageView picView = (ImageView) findViewById(R.id.picture);
                    picView.setImageBitmap(thePic);
                }
            }
        }
    
        /**
         * this function does the crop operation.
         */
        private void performCrop() {
            // take care of exceptions
            try {
                // call the standard crop action intent (the user device may not
                // support it)
                Intent cropIntent = new Intent("com.android.camera.action.CROP");
                // indicate image type and Uri
                cropIntent.setDataAndType(picUri, "image/*");
                // set crop properties
                cropIntent.putExtra("crop", "true");
                // indicate aspect of desired crop
                cropIntent.putExtra("aspectX", 2);
                cropIntent.putExtra("aspectY", 1);
                // indicate output X and Y
                cropIntent.putExtra("outputX", 256);
                cropIntent.putExtra("outputY", 256);
                // retrieve data on return
                cropIntent.putExtra("return-data", true);
                // start the activity - we handle returning in onActivityResult
                startActivityForResult(cropIntent, CROP_PIC);
            }
            // respond to users whose devices do not support the crop action
            catch (ActivityNotFoundException anfe) {
                Toast toast = Toast
                        .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    

    您可以使用以下简单教程进行裁剪:

    1. http://khurramitdeveloper.blogspot.in/2013/07/capture-or-select-from-gallery-and-crop.html
    2. http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/
    3. http://www.coderzheaven.com/2012/12/15/crop-image-android/
    4. http://shaikhhamadali.blogspot.in/2013/09/capture-images-and-crop-images-using.html

    【讨论】:

    • 此代码有效,我只需要保存一个问题来保存图像路径,但裁剪后的图像是否允许这样做?并且可以使用新的 uri 而不是显示图像的位图?
    • 我在这条线上遇到了 nullPointerException。 捆绑附加服务 = data.getExtras();。 extras 为空。我使用的是安卓 M 设备。
    • 不起作用!因为在 CAMERA_CAPTURE 之后 data.getData() 为空
    【解决方案2】:

    为自己节省大量时间并使用 this library 我自己在搞砸了,偶然发现了这个库,它使用起来非常简单,你会得到一个专业的图像裁剪视图,让你可以选择相机或照片库。

    简单示例:

    在你的 gradle 中包含这个库

    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
    

    向清单添加权限

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    //add this under <application>
    <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
      android:theme="@style/Base.Theme.AppCompat"/>
    

    在你的活动中

    //on button press or anywhere, this starts the image picking and cropping process
    CropImage.activity()
      .setGuidelines(CropImageView.Guidelines.ON)
      .start(this);
    
    //in your activity where you will get the result of your cropped image
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
          Uri resultUri = result.getUri();
    
          //From here you can load the image however you need to, I recommend using the Glide library
    
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
          Exception error = result.getError();
        }
      }
    }
    

    我不隶属于这个软件

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @MaxVollmer ive 添加了一个简单的例子
    • @Fonix 您提到的库非常好,但遗憾的是,它不再维护并且使用了已弃用的功能。如果您能推荐您知道的任何其他图书馆,那就太好了。
    • @James 很遗憾没有,因为我有一段时间不用对相机做任何事情了
    【解决方案3】:

    试试这个

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
                    mImageCaptureUri = Uri.fromFile(new File(Environment
                            .getExternalStorageDirectory(), "tmp_avatar_"
                            + String.valueOf(System.currentTimeMillis())
                            + ".jpg"));
    
                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                            mImageCaptureUri);
    
                    intent.setData(mImageCaptureUri);
                    intent.putExtra("outputX", 200);
                    intent.putExtra("outputY", 200);
                    intent.putExtra("aspectX", 1);
                    intent.putExtra("aspectY", 1);
                    intent.putExtra("scale", true);
                    intent.putExtra("return-data", true);   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      相关资源
      最近更新 更多