OpenCV - Android:java.lang.IllegalArgumentException:bmp == null [英] OpenCV - Android : java.lang.IllegalArgumentException: bmp == null
问题描述
我正在尝试从 JavaCameraView 捕获图像并将捕获的图像加载到另一个活动中,并且应该被处理(Hough Circles).
I'm trying to capture an image from the JavaCameraView and load the captured image into another activity and is supposed to be processed (Hough Circles).
private void takePhoto(final Mat rgba) {
// Determine the path and metadata for the photo.
final long currentTimeMillis = System.currentTimeMillis();
final String appName = getString(R.string.app_name);
final String galleryPath =
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
final String albumPath = galleryPath + File.separator +
appName;
final String photoPath = albumPath + File.separator +
currentTimeMillis + LabActivity.PHOTO_FILE_EXTENSION;
final ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, photoPath);
values.put(Images.Media.MIME_TYPE,
LabActivity.PHOTO_MIME_TYPE);
values.put(Images.Media.TITLE, appName);
values.put(Images.Media.DESCRIPTION, appName);
values.put(Images.Media.DATE_TAKEN, currentTimeMillis);
// Ensure that the album directory exists.
File album = new File(albumPath);
if (!album.isDirectory() && !album.mkdirs()) {
Log.e(TAG, "Failed to create album directory at " +
albumPath);
onTakePhotoFailed();
return;
}
/*
// Try to create the photo.
Imgproc.cvtColor(rgba, mBgr, Imgproc.COLOR_RGBA2BGR, 3);
if (!Imgcodecs.imwrite(photoPath, mBgr)) {
Log.e(TAG, "Failed to save photo to " + photoPath);
onTakePhotoFailed();
}
Log.d(TAG, "Photo saved successfully to " + photoPath);
*/
Mat grayMat = new Mat();
Mat cannyEdges = new Mat();
Mat lines = new Mat();
Imgproc.cvtColor(rgba, mBgr, Imgproc.COLOR_RGBA2BGR, 3);
//Converting the image to grayscale
Imgproc.cvtColor(mBgr, grayMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(grayMat, cannyEdges, 10, 100);
Imgproc.HoughLinesP(cannyEdges, lines, 1, Math.PI / 180, 50, 20, 20);
Mat houghLines = new Mat();
houghLines.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1);
//Drawing lines on the image
for (int i = 0; i < lines.cols(); i++) {
double[] points = lines.get(0, i);
double x1, y1, x2, y2;
x1 = points[0];
y1 = points[1];
x2 = points[2];
y2 = points[3];
Point pt1 = new Point(x1, y1);
Point pt2 = new Point(x2, y2);
//Drawing lines on an image
Imgproc.line(houghLines, pt1, pt2, new Scalar(255, 0, 0), 1);
}
//Converting Mat back to Bitmap
Utils.matToBitmap(houghLines, currentBitmap);
Log.d(TAG, "Photo saved successfully to " + photoPath);
// Try to insert the photo into the MediaStore.
Uri uri;
try {
uri = getContentResolver().insert(
Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (final Exception e) {
Log.e(TAG, "Failed to insert photo into MediaStore");
e.printStackTrace();
// Since the insertion failed, delete the photo.
File photo = new File(photoPath);
if (!photo.delete()) {
Log.e(TAG, "Failed to delete non-inserted photo");
}
onTakePhotoFailed();
return;
}
// Open the photo in LabActivity.
final Intent intent = new Intent(this, LabActivity.class);
intent.putExtra(LabActivity.EXTRA_PHOTO_URI, uri);
intent.putExtra(LabActivity.EXTRA_PHOTO_DATA_PATH,
photoPath);
runOnUiThread(new Runnable() {
@Override
public void run() {
startActivity(intent);
}
});
}
单击捕获选项后发生错误.
The error occurs after i click the capture option.
12-07 00:15:45.420 9205-9933/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-8672
Process: com.example.alexies.cameratesting, PID: 9205
java.lang.IllegalArgumentException: bmp == null
at org.opencv.android.Utils.matToBitmap(Utils.java:122)
at org.opencv.android.Utils.matToBitmap(Utils.java:132)
at com.example.alexies.cameratesting.MainActivity.takePhoto(MainActivity.java:380)
推荐答案
currentBitmap
在您的代码中为 null
.
要么您没有复制分配了位图值的部分,要么从未分配过.如果您的代码中缺少某些部分,请将其添加到您的问题中,如果没有,您的问题是您从未获得位图.
Either you didn't copy the part where it's assigned a bitmap value or it's never assigned. If there's some part of your code missing please add it in your question, if not, your problem is that you never get the bitmap.
您从不启动 currentBitmap
.docs 声明提供的位图必须与 Mat
对象(您的 houghLines
)和 您的位图类型 应该是 ARGB_8888
或 RGB_565代码>.
You never initiate currentBitmap
. The docs state that the provided bitmap must be the same size as the Mat
object (your houghLines
) and the type of your bitmap should be ARGB_8888
or RGB_565
.
这篇关于OpenCV - Android:java.lang.IllegalArgumentException:bmp == null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!