问题
I need convert a 1-channel iplimage (gray) in an Android Bitmap. I have:
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());
I think that the problem is in the order of channels, because with this code I get a translucent picture. Perhaps I need change the order of channels in "aux" to get ARGB order and check with Bitmap configuration (ARGB_8888). Is this possible?
回答1:
I've never used the OpenCV bindings for Android, but here's some code to get you started. Regard it as pseudocode, because I can't try it out... but you'll get the basic idea.
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;
}
来源:https://stackoverflow.com/questions/8807414/1-channel-iplimage-android-bitmap