我有一個代碼捕獲/選擇一個圖像,並將其設置爲一個imageview。我想要的是,將這個圖像存儲在一個變量中,將它傳遞給一個函數,並在textView中設置一個整數輸出。現在,如果我手動使用存儲在drawable中的圖像(轉換成位圖,然後mat),我會得到所需的結果。但是,當我嘗試使用從相機/圖庫中動態獲取的位圖圖像(轉換爲圖像後)時,會出現異常。據我所知,捕獲/選定的圖像沒有被轉換或存儲在一個變量,因爲它顯示bmp = null。 的代碼是: 對於捕捉圖像:在android中使用opencv
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);
}
十一月7日至25日: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:90) 在org.opencv.android.Utils.bitmapToMat(Utils.java:102) 在 融爲一體。 example.smartlayer.imageprocessing.MainActivity $ 2.onClick(MainActivity.java:80) at android.view.View.performClick(View.j AVA:4785) 在android.view.View $ PerformClick.run(View.java:19884) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(處理程序。 java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method ) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) at com.an droid.internal.os.ZygoteInit.main(ZygoteInit.java:700)
如果INT的問題,問的代碼或方式有差異,請讓我知道,我會糾正。
你說「** BMP = NULL **」。但是,在代碼中沒有看到「bmp」 - 請指出發生這種空圖像的位置。 –
它在標準的utils類,而不是我的代碼。它在logcat上顯示發生異常時的情況:** java.lang.IllegalArgumentException:bmp == null ** – Harshita
然後請發佈您的LogCat。當你跟蹤堆棧跟蹤時,代碼中的哪一行會給出錯誤? –