2012-10-30 63 views
12

我試圖隱蔽一個墊一個位圖使用下面的代碼:轉換墊位圖OpenCV進行Android的

Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    Mat tmp = new Mat (width,height,CvType.CV_8UC1,new Scalar(4)); 
    try { 
    //Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_RGB2BGRA); 
    Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_GRAY2RGBA, 4); 
    Utils.matToBitmap(tmp, bmp);} 
    catch (CvException e){Log.d("Exception",e.getMessage());} 

我seedsImage是墊目標。 和異常,並得到爲10-09 22:15:09.418: D/Exception(2461): ..\..\modules\java\generator\src\cpp\utils.cpp:105: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, _jclass*, jlong, _jobject*, jboolean) 我試圖通過搜索,但沒有辦法爲我工作。 能anynone幫助嗎?

+0

改爲CV_8UC1但它仍然得到錯誤 –

+0

從HTTP://計算器。 COM /問題/ 17390289 /轉換位圖到墊後使用,Android的攝像頭採集圖像, - 你看了Utils.matToBitmap(墊,BMP)的方法? – user1689757

回答

17

1)的OpenCV墊構造預計<rows, cols>對代替<width, height>作爲它的參數。所以,你有你的第二行改爲

Mat tmp = new Mat (height, width, CvType.CV_8U, new Scalar(4)); 

2)Imgproc.cvtColor可以改變tmp對象的尺寸。因此它是安全的顏色轉換後創建位圖:

Bitmap bmp = null; 
Mat tmp = new Mat (height, width, CvType.CV_8U, new Scalar(4)); 
try { 
    //Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_RGB2BGRA); 
    Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_GRAY2RGBA, 4); 
    bmp = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888); 
    Utils.matToBitmap(tmp, bmp); 
} 
catch (CvException e){Log.d("Exception",e.getMessage());} 
+0

我仍然有同樣的問題。有沒有錯誤的位圖配置? –

4

嘗試用於消此代碼到位圖的轉換

**Mat mRgba; 
public void onCameraViewStarted(int width, int height) { 
     mRgba = new Mat(height, width, CvType.CV_8UC4); 
} 
public Mat onCameraFrame(Mat inputFrame) { 
    inputFrame.copyTo(mRgba); 
    return mRgba; 
} 
private void captureBitmap(){ 
     bitmap = Bitmap.createBitmap(mOpenCvCameraView.getWidth()/4,mOpenCvCameraView.getHeight()/4, Bitmap.Config.ARGB_8888); 
    try { 
      bitmap = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888); 
       Utils.matToBitmap(mRgba, bitmap); 
       mBitmap.setImageBitmap(bitmap); 
       mBitmap.invalidate(); 
    }catch(Exception ex){ 
      System.out.println(ex.getMessage()); 
    } 
}** 
+0

我相信你的代碼將只的情況下,當我們使用'CvCameraViewListener2'。但是,如果它是一個普遍的情況下,我只想寫一個方法! – Mayank