[英]using opencv in android
我有一個捕獲/選擇圖像並將其設置為imageview的代碼。 我想要的是,將此圖像存儲在變量中,將其傳遞給函數,並在textView中設置整數輸出。 現在,如果我手動使用存儲在drawable中的圖像(轉換為位圖,然后轉換為mat),則會得到所需的結果。 但是,當我嘗試使用從攝影機/畫廊動態獲取的位圖圖像(轉換為墊子后)時,會出現異常。 據我了解,捕獲/選擇的圖像沒有被轉換或存儲在變量中,因為它顯示bmp = null。 這些代碼是:要捕獲圖像:
public void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
thumbnail1=thumbnail;
ivImage.setImageBitmap(thumbnail);
}
選擇圖像:
public void onSelectFromGalleryResult(Intent data) {
bm = null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ivImage.setImageBitmap(bm);
}
為了處理圖像並設置輸出:
public void countDents(Mat src) {
int count = 0;
Mat source;
source = src;
int cnt = 0, cnt2 = 0;
Mat middle, destination1, destination2;
List<MatOfPoint> contours1 = new ArrayList<>();
List<MatOfPoint> contours2 = new ArrayList<>();
middle = new Mat();
destination1 = new Mat();
destination2 = new Mat();
/*Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.car);
Utils.bitmapToMat(img, source);*/
Imgproc.cvtColor(source, middle, Imgproc.COLOR_RGB2GRAY);
Imgproc.equalizeHist(middle, middle);
// ivImage.setImageBitmap(img);
Imgproc.threshold(middle, destination1, 150, 255, Imgproc.THRESH_BINARY);
Imgproc.threshold(middle, destination2, 150, 255, Imgproc.THRESH_BINARY_INV);
Imgproc.findContours(destination1, contours1, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.findContours(destination2, contours2, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
// Utils.matToBitmap(destination2, img);
for (int i = 0; i < contours1.size(); i++) {
if (Imgproc.contourArea(contours1.get(i)) > 5 && Imgproc.contourArea(contours1.get(i)) < 200) {
cnt++;
}
}
for (int i = 0; i < contours2.size(); i++) {
if (Imgproc.contourArea(contours2.get(i)) > 5 && Imgproc.contourArea(contours2.get(i)) < 200) {
cnt2++;
count = cnt + cnt2;
String c = Integer.toString(count);
TVdents.setText(c);
}
}
調用函數的onClick函數:
public void onClick(View v) {
Utils.bitmapToMat(thumbnail1,tmp);
Utils.bitmapToMat(bm,tmp);
countDents(tmp);
}
07-25 11:19:21.727 19363-19363 / com.example.smartlayer.imageprocessing E / AndroidRuntime:致命例外:主進程:com.example.smartlayer.imageprocessing,PID:19363 java.lang.IllegalArgumentException:bmp == null在org.opencv.android.Utils.bitmapToMat(Utils.java:102)在org.opencv.android.Utils.bitmapToMat(Utils.java:90)在com.example.smartlayer.imageprocessing.MainActivity $ 2.onClick(MainActivity。的android.view.View.performClick(View.java:4785)的java:80)的android.os.Handler.handleCallback(Handler.java:739)的android.view.View $ PerformClick.run(View.java:19884)的)在android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThread.java:5343)在java.lang com上java.lang.reflect.Method.invoke(Method.java:372)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)的.reflect.Method.invoke(本機方法) .android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
如果問題,代碼或詢問方式有任何出入,請告訴我,我會糾正。
實際上,問題是thumbnail1
為null
; 如果是tmp
,則根據GitHub上的源代碼,您將看到java.lang.IllegalArgumentException: mat == null
。
所以,我認為問題在於賦值后在onCaptureImageResult()
:
thumbnail1 = thumbnail;
事情被垃圾收集了, thumbnail1
不再有效。 所以,你能做的就是副本thumbnail
通過這樣做,而不是 :
thumbnail1 = thumbnail.copy(thumbnail.getConfig(), true);
(不過,我不是100%肯定會奏效)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.