来自相机的Android裁剪照片

时间:2014-07-14 10:03:02

标签: android

我想要从相机拍摄的裁剪照片,到目前为止,我尝试这样做,但没有成功

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);

                getActivity().startActivityForResult(intent, REQUEST_CAMERA);

是否可以在没有任何第3部分库的情况下完成。 我查了https://github.com/biokys/cropimage

但它没有给我任何结果

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            String path = data.getStringExtra(CropImage.IMAGE_PATH);

            // if nothing received
            if (path == null) {

                return;
            }

            // cropped bitmap
            Bitmap bitmap = BitmapFactory.decodeFile(path);

            ((ImageView) findViewById(R.id.userpicture)).setImageBitmap(bitmap);
        }

2 个答案:

答案 0 :(得分:0)

  

是否可以在没有任何第3部分库的情况下进行。?

不可靠。 Android does not have a CROP Intent

  

我查了https://github.com/biokys/cropimage,但它没有给我任何结果

也许你没有正确地整合它。除了我的博文(上面链接)中提到的库,the Android Arsenal has a few options

答案 1 :(得分:0)

试试这个

private void performCrop(Uri picUri) {
        try {

            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", 1);
            cropIntent.putExtra("aspectY", 1);
            // indicate output X and Y
            cropIntent.putExtra("outputX", 128);
            cropIntent.putExtra("outputY", 128);
            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);
        }
        // respond to users whose devices do not support the crop action
        catch (ActivityNotFoundException anfe) {
            // display an error message
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

声明:

final int PIC_CROP = 1;

在顶部。

在onActivity结果方法中,编写以下代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PIC_CROP) {
        if (data != null) {
            // get the returned data
            Bundle extras = data.getExtras();
            // get the cropped bitmap
            Bitmap selectedBitmap = extras.getParcelable("data");

            imgView.setImageBitmap(selectedBitmap);
        }
    }

}