实现调用相机拍照之后裁剪功能源码

实现调用相机拍照之后裁剪功能源码

在移动应用开发中,调用相机拍照并进行裁剪是很常见的需求。本文将介绍如何实现调用相机拍照之后进行裁剪的功能,并提供相应的源码。

首先,在AndroidManifest.xml文件中添加以下权限:

```xml

```

然后,在布局文件中添加一个ImageView和一个Button,用于显示拍照后的图片和触发拍照事件:

```xml

android:id="@+id/imageView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scaleType="centerCrop"/>

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Take Photo"/>

```

接着,在Activity中实现以下代码:

```java

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_IMAGE_CAPTURE = 1;

private static final int REQUEST_IMAGE_CROP = 2;

private static final String FILE_PROVIDER_AUTHORITY = "com.example.myapplication.fileprovider";

private Uri mImageUri;

private ImageView mImageView;

private Button mButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mImageView = findViewById(R.id.imageView);

mButton = findViewById(R.id.button);

mButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

dispatchTakePictureIntent();

}

});

}

private void dispatchTakePictureIntent() {

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

File photoFile = null;

try {

photoFile = createImageFile();

} catch (IOException ex) {

ex.printStackTrace();

}

if (photoFile != null) {

mImageUri = FileProvider.getUriForFile(this, FILE_PROVIDER_AUTHORITY, photoFile);

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);

startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

}

}

}

private File createImageFile() throws IOException {

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());

String imageFileName = "JPEG_" + timeStamp + "_";

File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);

return imageFile;

}

@Override

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

super.onActivityResult(requestCode, resultCode, data);

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

cropImage();

} else if (requestCode == REQUEST_IMAGE_CROP && resultCode == RESULT_OK) {

Bundle extras = data.getExtras();

Bitmap bitmap = extras.getParcelable("data");

mImageView.setImageBitmap(bitmap);

}

}

private void cropImage() {

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

cropIntent.setDataAndType(mImageUri, "image/*");

cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

cropIntent.putExtra("crop", "true");

cropIntent.putExtra("aspectX", 1);

cropIntent.putExtra("aspectY", 1);

cropIntent.putExtra("outputX", 256);

cropIntent.putExtra("outputY", 256);

cropIntent.putExtra("return-data", true);

startActivityForResult(cropIntent, REQUEST_IMAGE_CROP);

}

}

```

代码中,dispatchTakePictureIntent()方法用于启动相机拍照,createImageFile()方法用于创建临时文件保存图片,onActivityResult()方法用于处理拍照和裁剪的结果,cropImage()方法用于启动裁剪界面。

最后,在res/xml/目录下创建file_paths.xml文件,用于指定临时文件的存储路径:

```xml

```

以上就是实现调用相机拍照之后进行裁剪的全部代码。通过此方法,可以快速、方便地实现调用相机拍照并进行裁剪的功能。