|
public static Bitmap convertMatToBitmap(Mat image) {
Bitmap bitmap = null;
try {
int width = image.width();
int height = image.height();
int channels = image.channels();
byte[] pixels = new byte[width * height * channels];
image.get(0, 0, pixels);
if (channels == 2) {
// CV_8UC2
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int i = 0; i < width * height; i++) {
int r = pixels[i * 2] & 0xff;
int g = pixels[i * 2 + 1] & 0xff;
bitmap.setPixel(i % width, i / width, Color.argb(255, r, g, 0));
}
} else if (channels == 3) {
// CV_8UC3
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_888);
for (int i = 0; i < width * height; i++) {
int r = pixels[i * 3] & 0xff;
int g = pixels[i * 3 + 1] & 0xff;
int b = pixels[i * 3 + 2] & 0xff;
bitmap.setPixel(i % width, i / width, Color.rgb(r, g, b));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
|
|