Android 4.2.2中的Android裁剪图像

时间:2013-05-05 08:41:49

标签: android

我想从相机拍摄后裁剪图像。 我正在使用此代码。

Intent cropIntent = new Intent("com.android.camera.action.CROP");

但它给出了错误: 找不到活动。 我的Android版本是4.2.2。

我也试过了。

Intent intent = new Intent(Intent.ACTION_PICK);

但它会打开图库而不是捕获的图像。

2 个答案:

答案 0 :(得分:0)

使用以下代码,这将对您有所帮助。

    //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", 1);
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, PIC_CROP);

了解更多详情 http://mobile.tutsplus.com/tutorials/android/capture-and-crop-an-image-with-the-device-camera/

答案 1 :(得分:0)

以下是我的三星Galaxy s3。检查快照

使用此功能拍摄带照相机的图像

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher" />

 </LinearLayout>

MainActivity

public class MainActivity extends Activity {

private static final int PICK_FROM_CAMERA = 1; 
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.imageView1);
    Button buttonCamera = (Button) findViewById(R.id.button1);
    buttonCamera.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
        // call android default camera
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT,
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
        // ******** code for crop image
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 0);
        intent.putExtra("aspectY", 0);
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 150);

        try {

        intent.putExtra("return-data", true);
        startActivityForResult(intent, PICK_FROM_CAMERA);

        } catch (ActivityNotFoundException e) {

        }
        }
        });


}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == PICK_FROM_CAMERA) {
    Bundle extras = data.getExtras();
    if (extras != null) {
    Bitmap photo = extras.getParcelable("data");
    iv.setImageBitmap(photo);

    }
    }       
    }        
    } 

产生快照

enter image description here