Java 如何从相机和多媒体资料中拾取图像,裁剪图像并设置为“图像视图”
请帮助我,到目前为止,我已经编写了一个代码,可以从相机和多媒体资料中选择图像并显示出来,但我发现很难裁剪它。我正在尝试使用第三方android库,名为android image cropper,但发现很难实现。我对代码进行了研究,但没有发现任何有用的东西。换言之,我正试图在whasap中实现可获得的效果,在whasap中,您单击一个按钮上载配置文件图片,它将其设置为裁剪,然后显示图像的圆形视图。这里是到android库的链接。任何帮助都将不胜感激,谢谢大家 这是我的java代码Java 如何从相机和多媒体资料中拾取图像,裁剪图像并设置为“图像视图”,java,android,xml,imageview,Java,Android,Xml,Imageview,请帮助我,到目前为止,我已经编写了一个代码,可以从相机和多媒体资料中选择图像并显示出来,但我发现很难裁剪它。我正在尝试使用第三方android库,名为android image cropper,但发现很难实现。我对代码进行了研究,但没有发现任何有用的东西。换言之,我正试图在whasap中实现可获得的效果,在whasap中,您单击一个按钮上载配置文件图片,它将其设置为裁剪,然后显示图像的圆形视图。这里是到android库的链接。任何帮助都将不胜感激,谢谢大家 这是我的java代码 public c
public class MainActivity extends Activity {
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private Button btnSelect;
private ImageView ivImage;
private String userChoosenTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
btnSelect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
ivImage = (ImageView) findViewById(R.id.ivImage);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(userChoosenTask.equals("Take Photo"))
cameraIntent();
else if(userChoosenTask.equals("Choose from Library"))
galleryIntent();
} else {
//code for deny
}
break;
}
}
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result=Utility.checkPermission(MainActivity.this);
if (items[item].equals("Take Photo")) {
userChoosenTask ="Take Photo";
if(result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask ="Choose from Library";
if(result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
// Ezphoto picker in action
private void galleryIntent()
{
EZPhotoPickConfig config = new EZPhotoPickConfig();
config.photoSource = PhotoSource.GALERY;
config.needToExportThumbnail = true;
config.exportingThumbSize = 200;
config.exportingSize = 1000;
EZPhotoPick.startPhotoPickActivity(MainActivity.this, config);
}
private void cameraIntent()
{
EZPhotoPickConfig config = new EZPhotoPickConfig();
config.photoSource = PhotoSource.CAMERA;
config.exportingSize = 1000;
EZPhotoPick.startPhotoPickActivity(MainActivity.this, config);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK){
return;
}
if(requestCode == EZPhotoPick.PHOTO_PICK_REQUEST_CODE){
try {
Bitmap pickedPhoto = new EZPhotoPickStorage(this).loadLatestStoredPhotoBitmapThumbnail();
ivImage.setImageBitmap(pickedPhoto);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp" >
<Button
android:id="@+id/btnSelectPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Photo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
来源:
这是密码
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties here
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", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
宣布:
final int PIC_CROP = 1;
在顶端
在onActivity结果方法中,编写以下代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
imgView.setImageBitmap(selectedBitmap);
}
}
}
资料来源:
其他有用的链接:
希望有帮助!祝你好运 您可以尝试使用此库来裁剪图像
如果你喜欢我的建议,请投赞成票。@S Kharbanda请再说一句,我在哪里声明最终int PIC_CROP=1;声明
int PIC\u CROP=1
在声明private int REQUEST\u CAMERA=0的顶部,选择\u FILE=1代码>。就像那样,但这只是1个变量,而不是2个。没问题,如果你能投票接受我的答案,我会非常感激。谢谢