gpt4 book ai didi

android - 如何获取裁剪后的图像并将其显示在 ImageView 中?

转载 作者:行者123 更新时间:2023-11-30 05:12:41 25 4
gpt4 key购买 nike

我正在使用 Android Image Cropper Android Image Cropper .我已经有了以下代码,可以从图库或相机中选择图像,https://github.com/ArthurHub/Android-Image-Cropper/wiki/Pick-image-for-cropping-from-Camera-or-Gallery .

这是我当前的代码:

主要 Activity .java:

public class MainActivity extends AppCompatActivity {
private Button changeImageButton;
private Uri mCropImageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeImageButton = (Button)findViewById(R.id.changeImage);
changeImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSelectImageClick(v);
}
});


}
@Override
@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle result of pick image chooser
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);

// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
mCropImageUri = imageUri;

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already granted, can start crop image activity
startCropImageActivity(imageUri);
}
}
}

public void onSelectImageClick(View view) {
CropImage.startPickImageActivity(this);
}

private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri).setCropShape(CropImageView.CropShape.RECTANGLE).setFixAspectRatio(true)
.start(this);

}

}

这是 activity_main xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<Button
android:id="@+id/changeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Change Image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />



</android.support.constraint.ConstraintLayout>

效果很好,当我点击按钮时,我会看到一个对话框,询问我要从哪里拍摄图像:画廊、相机、文件等。当我选择其中一个时,我会进入裁剪 Activity 。但是在我点击裁剪之后,没有任何反应。如何在现有代码中获取裁剪后的图像,并将其设置为正方形 ImageView ,比方说 150x150dp 大小。我不知道我应该使用什么回调来保存裁剪/旋转的图像?因为这个裁剪器似乎将几个 API 版本混合在一起,所以我很困惑我需要怎么做。帮助将不胜感激

最佳答案

private ImageView open_gallary;

在你的 OnCreate()

open_gallary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
permission();
}else {
CropImage.activity()
.start(getContext(), Register.this);

}


}
});

你的权限方法是:

   public void permission() {
final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_permission);
dialog.show();
Button cancel_log, open;
ImageView close_dialog;
open = (Button) dialog.findViewById(R.id.open);
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + getActivity().getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
getActivity().startActivity(i);

}
});

cancel_log = (Button) dialog.findViewById(R.id.cancel_log);
cancel_log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
close_dialog = (ImageView) dialog.findViewById(R.id.close_dialog);
close_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
})
}

并设置你的图像使用这个:

 Uri uri;

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
uri = result.getUri();
open_gallary.setImageURI(uri);////set the image after taking it
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}

别忘了

<uses-permission android:name="android.permission.CAMERA" />

关于android - 如何获取裁剪后的图像并将其显示在 ImageView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53461207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com