代碼工作正常,代碼會有小的變化。
private void showDialog(File pictureFile) {
// custom dialog
final Dialog dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.image_dialog);
dialog.setTitle("Image");
// find the imageview and draw it!
ImageView image = (ImageView) dialog.findViewById(R.id.image);
Bitmap thumbnail;
try {
thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), Uri.fromFile(pictureFile));
cropImage(image, thumbnail);
// Imgproc.threshold(tmp, tmp, 60, 100, Imgproc.THRESH_BINARY);
// Utils.matToBitmap(tmp, image1);
// Imgproc.threshold(tmp, tmp, 1, 255, Imgproc.THRESH_OTSU);
//using floodfill and watershed to remove noise
// Mat mask = new Mat(tmp.rows() + 2, tmp.cols() + 2, CvType.CV_8UC1);
// Imgproc.floodFill(tmp, mask, new Point(tmp.cols() - 10, 10), new Scalar(255.0, 255.0, 255));
image.setImageBitmap(thumbnail);
} catch (IOException e) {
e.printStackTrace();
}
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
private void cropImage(ImageView image, Bitmap thumbnail) {
Mat src = new Mat();
Utils.bitmapToMat(thumbnail, src);
//converts mat color
Imgproc.cvtColor(src, src, Imgproc.COLOR_RGBA2BGR);
Rect roi = new Rect(40, 100, 100, 120);
Mat cropped = new Mat(src, roi);
Bitmap tempBmp1 = Bitmap.createBitmap(100, 120, Bitmap.Config.ARGB_8888);
Utils.matToBitmap(cropped, tempBmp1);
image.setImageBitmap(tempBmp1);
}
需要從ImageMat中裁剪一部分,即sourceMat並在imageView上顯示它。 –
感謝您的回覆@Antonio,並在openCVLibrary320和android sdk版本25上工作。 –
我也會嘗試'Bitmap.createBitmap(40,40,RGB_565);'什麼是縮略圖? – Antonio