【问题标题】:Android crop image in android 4.2.2Android 4.2.2 中的 Android 裁剪图像
【发布时间】:2013-05-05 08:41:48
【问题描述】:

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

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

但它给出了错误: 未找到活动。 我的安卓版本是4.2.2。

我也试过了。

Intent intent = new Intent(Intent.ACTION_PICK);

但它打开画廊而不是捕获的图像。

【问题讨论】:

标签: android


【解决方案1】:

使用下面的代码会有所帮助。

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

【讨论】:

    【解决方案2】:

    以下内容适用于我的三星 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>
    

    主活动

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

    生成的快照

    【讨论】:

    • 我使用了您的代码。 java.lang.RuntimeException:将结果 ResultInfo{who=null, request=1, result=0, data=null} 传递给活动 {com.example.crop/com.example.crop.MainActivity} 失败:java.lang.NullPointerException
    • 你能贴出代码吗?因为它可以在我的手机上运行,​​我运行的是 4.1.2。你在什么设备上测试?
    • 我正在模拟器上测试它。
    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 2018-09-20
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    相关资源
    最近更新 更多