如何裁剪从摄像头图像 [英] How to crop images from camera

查看:270
本文介绍了如何裁剪从摄像头图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以裁剪摄像机的图像。现在,它正显示出作物的形象和选择作物的部分,而在保存按钮后自来水。其表现为保存图像。之后,没有什么发生。这里是我的code。

按钮点击:

 意向意图=新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
intent.putExtra(裁剪,真);
intent.putExtra(aspectX,0);
intent.putExtra(aspectY,0);
intent.putExtra(outputX,200);
intent.putExtra(outputY,150);
intent.putExtra(回归数据,真正的);
startActivityForResult(意向,CAMERA_PIC_REQUEST);

的onActivityResult:

 捆绑额外= data.getExtras();
位图位=(位图)extras.get(数据);
如果(位图!= NULL){
    Img_View.setImageBitmap(位图);
}


解决方案

您可以使用此code进行裁剪:

  .....
最终诠释CAMERA_CAPTURE = 1;
最终诠释CROP_PIC = 2;
私人乌里picUri;
....
@覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        按钮captureBtn =(按钮)findViewById(R.id.capture_btn);
        captureBtn.setOnClickListener(本);
    }    公共无效的onClick(视图v){
        如果(v.getId()== R.id.capture_btn){
            尝试{
                //使用标准的意图拍摄图像
                意图captureIntent =新意图(
                        MediaStore.ACTION_IM​​AGE_CAPTURE);
                //我们会处理的onActivityResult返回的数据
                startActivityForResult(captureIntent,CAMERA_CAPTURE);
            }赶上(ActivityNotFoundException anfe){
                吐司面包= Toast.makeText(这一点,此设备不支持作物行动了!
                        Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK){
            如果(要求code == CAMERA_CAPTURE){
                //得到的URI所捕获的图像
                picUri = data.getData();
                performCrop();
            }
            //用户从裁剪图像返回
            否则,如果(要求code == CROP_PIC){
                //获取返回的数据
                捆绑额外= data.getExtras();
                //获取裁剪位图
                位图thePic = extras.getParcelable(数据);
                ImageView的picView =(ImageView的)findViewById(R.id.picture);
                picView.setImageBitmap(thePic);
            }
        }
    }    / **
     *这个函数作物操作。
     * /
    私人无效performCrop(){
        //照顾异常
        尝试{
            //调用标准动作作物意图(用户设备可能不
            //支持它)
            意图cropIntent =新意图(com.android.camera.action.CROP);
            //显示图像类型和URI
            cropIntent.setDataAndType(picUri,图像/ *);
            //设置裁剪性能
            cropIntent.putExtra(裁剪,真);
            //注明所需作物方面
            cropIntent.putExtra(aspectX,2);
            cropIntent.putExtra(aspectY,1);
            //指示输出X和Y
            cropIntent.putExtra(outputX,256);
            cropIntent.putExtra(outputY,256);
            //检索返回数据
            cropIntent.putExtra(回归数据,真正的);
            //启动活动 - 我们处理返回的的onActivityResult
            startActivityForResult(cropIntent,CROP_PIC);
        }
        //响应用户的设备不支持作物行动
        赶上(ActivityNotFoundException anfe){
            吐司面包吐司=
                    .makeText(这一点,此设备不支持作物行动了!,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.$c$crzheaven.com/2012/12/15/crop-image-android/

  4. http://shaikhhamadali.blogspot.in/2013/09/capture-images-and-crop-images-using.html

How can I crop the camera images. Now it is showing the the image for crop and after selecting the crop section while tap on the "Save" button. Its showing as "saving image". After that nothing is happen. Here is my code.

Button click :

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

解决方案

You can use this code to perform cropping:

.....
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();
        }
    }

You can use following simple tutorial to perform cropping:

  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

这篇关于如何裁剪从摄像头图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆