在上一篇文章中,我们讨论了如何从Android的Gallery中选择图像,但是在此项目中,没有裁切功能。有时我们会在手机上拍照,并希望将其更新为个人资料照片。但是我们需要删除背景。因此,在这种情况下,我们可以使用裁剪图像功能删除该背景,然后再上传该图像。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:将依赖项添加到build.gradle(Module:app)文件中
导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。
// This library is used for crop image feature
api ‘com.theartofdev.edmodo:android-image-cropper:2.8.+’
// This library is used for loading the
// cropped image into ImageView.
implementation ‘com.squareup.picasso:picasso:2.5.2’
步骤3:使用AndroidManifest.xml文件
将以下权限添加到AndroidManifest.xml文件。
在
android:name=”com.theartofdev.edmodo.cropper.CropImageActivity” android:theme=”@style/Base.Theme.AppCompat” />
以下是完整的AndroidManifest.xml文件的代码。
XML
XML
Java
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.squareup.picasso.Picasso;
import com.theartofdev.edmodo.cropper.CropImage;
public class MainActivity extends AppCompatActivity {
ImageView userpic;
private static final int GalleryPick = 1;
private static final int CAMERA_REQUEST = 100;
private static final int STORAGE_REQUEST = 200;
private static final int IMAGEPICK_GALLERY_REQUEST = 300;
private static final int IMAGE_PICKCAMERA_REQUEST = 400;
String cameraPermission[];
String storagePermission[];
Uri imageuri;
TextView click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Here we are initialising
// the text and image View
click = findViewById(R.id.click);
userpic = findViewById(R.id.set_profile_image);
// allowing permissions of gallery and camera
cameraPermission = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
storagePermission = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
// After clicking on text we will have
// to choose whether to
// select image from camera and gallery
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showImagePicDialog();
}
});
}
private void showImagePicDialog() {
String options[] = {"Camera", "Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick Image From");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
if (!checkCameraPermission()) {
requestCameraPermission();
} else {
pickFromGallery();
}
} else if (which == 1) {
if (!checkStoragePermission()) {
requestStoragePermission();
} else {
pickFromGallery();
}
}
}
});
builder.create().show();
}
// checking storage permissions
private Boolean checkStoragePermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == (PackageManager.PERMISSION_GRANTED);
return result;
}
// Requesting gallery permission
private void requestStoragePermission() {
requestPermissions(storagePermission, STORAGE_REQUEST);
}
// checking camera permissions
private Boolean checkCameraPermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == (PackageManager.PERMISSION_GRANTED);
boolean result1 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == (PackageManager.PERMISSION_GRANTED);
return result && result1;
}
// Requesting camera permission
private void requestCameraPermission() {
requestPermissions(cameraPermission, CAMERA_REQUEST);
}
// Requesting camera and gallery
// permission if not given
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case CAMERA_REQUEST: {
if (grantResults.length > 0) {
boolean camera_accepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean writeStorageaccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (camera_accepted && writeStorageaccepted) {
pickFromGallery();
} else {
Toast.makeText(this, "Please Enable Camera and Storage Permissions", Toast.LENGTH_LONG).show();
}
}
}
break;
case STORAGE_REQUEST: {
if (grantResults.length > 0) {
boolean writeStorageaccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (writeStorageaccepted) {
pickFromGallery();
} else {
Toast.makeText(this, "Please Enable Storage Permissions", Toast.LENGTH_LONG).show();
}
}
}
break;
}
}
// Here we will pick image from gallery or camera
private void pickFromGallery() {
CropImage.activity().start(MainActivity.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
Picasso.with(this).load(resultUri).into(userpic);
}
}
}
}
步骤3:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML格式
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.squareup.picasso.Picasso;
import com.theartofdev.edmodo.cropper.CropImage;
public class MainActivity extends AppCompatActivity {
ImageView userpic;
private static final int GalleryPick = 1;
private static final int CAMERA_REQUEST = 100;
private static final int STORAGE_REQUEST = 200;
private static final int IMAGEPICK_GALLERY_REQUEST = 300;
private static final int IMAGE_PICKCAMERA_REQUEST = 400;
String cameraPermission[];
String storagePermission[];
Uri imageuri;
TextView click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Here we are initialising
// the text and image View
click = findViewById(R.id.click);
userpic = findViewById(R.id.set_profile_image);
// allowing permissions of gallery and camera
cameraPermission = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
storagePermission = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
// After clicking on text we will have
// to choose whether to
// select image from camera and gallery
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showImagePicDialog();
}
});
}
private void showImagePicDialog() {
String options[] = {"Camera", "Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick Image From");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
if (!checkCameraPermission()) {
requestCameraPermission();
} else {
pickFromGallery();
}
} else if (which == 1) {
if (!checkStoragePermission()) {
requestStoragePermission();
} else {
pickFromGallery();
}
}
}
});
builder.create().show();
}
// checking storage permissions
private Boolean checkStoragePermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == (PackageManager.PERMISSION_GRANTED);
return result;
}
// Requesting gallery permission
private void requestStoragePermission() {
requestPermissions(storagePermission, STORAGE_REQUEST);
}
// checking camera permissions
private Boolean checkCameraPermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == (PackageManager.PERMISSION_GRANTED);
boolean result1 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == (PackageManager.PERMISSION_GRANTED);
return result && result1;
}
// Requesting camera permission
private void requestCameraPermission() {
requestPermissions(cameraPermission, CAMERA_REQUEST);
}
// Requesting camera and gallery
// permission if not given
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case CAMERA_REQUEST: {
if (grantResults.length > 0) {
boolean camera_accepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean writeStorageaccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (camera_accepted && writeStorageaccepted) {
pickFromGallery();
} else {
Toast.makeText(this, "Please Enable Camera and Storage Permissions", Toast.LENGTH_LONG).show();
}
}
}
break;
case STORAGE_REQUEST: {
if (grantResults.length > 0) {
boolean writeStorageaccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (writeStorageaccepted) {
pickFromGallery();
} else {
Toast.makeText(this, "Please Enable Storage Permissions", Toast.LENGTH_LONG).show();
}
}
}
break;
}
}
// Here we will pick image from gallery or camera
private void pickFromGallery() {
CropImage.activity().start(MainActivity.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
Picasso.with(this).load(resultUri).into(userpic);
}
}
}
}
输出:
Github链接: https : //github.com/Anni1123/CropImage