如何在一次活动中拍摄后裁剪图像

时间:2016-06-14 16:28:57

标签: java android

我有代码在那里从我的Android设备启动相机,我想添加活动的功能,以便能够按照用户的意愿裁剪图像。

public class MainActivity extends AppCompatActivity {

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Creating the onClick Listener for the Scan Button
    Button scanButton = (Button) findViewById(R.id.ScanButton);

    scanButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){

            //Creating an Intent to take a picture and return control to the calling application.
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            //Launching the intent.
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    });



}

}

1 个答案:

答案 0 :(得分:0)

您将使用与拍摄照片相同的方式使用其他Intent

Intent crop = new Intent("com.android.camera.action.CROP");
crop.setDataAndType(URI_OF_PIC_FROM_LAST_INTENT, "image/*");
crop.putExtra("crop", "true");
...
startActivityForResult(crop, REQUEST_CROP);

URI_OF_PIC_FROM_LAST_INTENTREQUEST_CROP替换为您自己的值。

Here's an example with more detail.

您也可以通过意图指定宽高比等内容。