如何从相机裁剪图像

用户名

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

点击按钮:

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);
}
Himanshu Agarwal

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

.....
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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用mogrify裁剪图像

来自分类Dev

如何使用ImageMagick提高裁剪图像的性能?

来自分类Dev

图像翻译被裁剪(如何防止)

来自分类Dev

如何使用drawImage()裁剪图像?

来自分类Dev

如何借助模板坐标裁剪图像

来自分类Dev

如何设置相机图像方向?

来自分类Dev

不使用位图裁剪相机拍摄的图像

来自分类Dev

如何从Instagram API获取裁剪的图像?

来自分类Dev

从图库中拾取时图像被裁剪,但从相机中单击时图像未被裁剪

来自分类Dev

如何从图像中裁剪出最大的矩形

来自分类Dev

如何快速在PhotoLibrary或相机中裁剪4 * 3图像

来自分类Dev

如何使用AsciiDoctor裁剪/裁剪图像

来自分类Dev

如何从Android 7.0中的相机或图库中选取要裁剪的图像?

来自分类Dev

如何从图像中裁剪边框/空白?

来自分类Dev

如何裁剪加载了SOIL的图像

来自分类Dev

使用相机会话拍摄的裁剪图像

来自分类Dev

如何在CameraX上的相机预览中裁剪图像矩形

来自分类Dev

如何调整裁剪图像的尺寸?

来自分类Dev

如何裁剪图像宽度svg?

来自分类Dev

如何裁剪图像并设置其与原始图像的裁剪范围

来自分类Dev

从相机胶卷导入图像并裁剪

来自分类Dev

如何在SurfaceView Android上制作裁剪相机视图

来自分类Dev

如何裁剪图像并保存?

来自分类Dev

如何使用CSS裁剪图像

来自分类Dev

调整相机拍摄的图像大小而不会裁剪

来自分类Dev

Android裁剪相机图片

来自分类Dev

如何从相机预览(表面视图)中裁剪图像并保存它,而不是像instagram app那样捕获实际的bt相机图像?

来自分类Dev

在 NiFi 中如何裁剪图像?

来自分类Dev

如何从相机获取图像

Related 相关文章

热门标签

归档