2012-01-10 59 views
1

我需要在Android位圖中轉換1通道iplimage(灰色)。我有:1 channel iplimage - > Android位圖

IplImage aux = IplImage.create(senal_gray.width, senal_gray.height, IPL_DEPTH_8U, 4); 
cvCvtColor(senal_gray, aux, CV_GRAY2BGRA); 
Bitmap bm = Bitmap.createBitmap(aux.width, aux.height, Bitmap.Config.ARGB_8888); 
bm.copyPixelsFromBuffer(aux.getByteBuffer()); 

我認爲問題是在渠道的順序,因爲與此代碼我得到一個半透明的圖片。也許我需要改變「aux」中的通道順序來獲取ARGB順序並檢查位圖配置(ARGB_8888)。這可能嗎?

回答

0

我從來沒有使用Android的OpenCV綁定,但這裏有一些代碼讓你開始。把它當作僞代碼,因爲我無法嘗試......但你會得到基本的想法。

public static Bitmap IplImageToBitmap(IplImage src) { 
    int width = src.width; 
    int height = src.height; 
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    for(int r=0;r<height;r++) { 
     for(int c=0;c<width;c++) { 
      int gray = (int) Math.floor(cvGet2D(src,r,c).getVal(0)); 
      bitmap.setPixel(c, r, Color.argb(255, gray, gray, gray)); 
     } 
    } 
    return bitmap; 
}