我正在尝试从位图(从矩阵创建)中创建和保存Alpha蒙版。由于某些原因,尝试将Alpha纹理(ALPHA_8配置格式)保存到磁盘失败。甚至可以将掩码保存到磁盘吗?
代码如下:
Mat image1 = new Mat(8, 16, CV_8UC4, new Scalar(0, 0, 0, 0));
Mat image2 = new Mat(2, 16, CV_8UC4, new Scalar(0, 0, 0, 0));
Mat image3 = new Mat(4, 6, CV_8UC4, new Scalar(0, 0, 0, 0));
Mat image4 = new Mat(4, 4, CV_8UC4, new Scalar(255, 255, 255, 255));
List<Mat> src = Arrays.asList(image3, image4, image3);
Mat dst1 = new Mat();
Core.hconcat(src, dst1);
List<Mat> src2 = Arrays.asList(image1, image2, dst1, image2);
Mat dst = new Mat();
Core.vconcat(src2, dst);
Bitmap bmp = Bitmap.createBitmap(dst.cols(), dst.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dst, bmp); // dst is my openCV matrix
Bitmap alpha = bmp.extractAlpha();
// alpha bitmap shows perfectly in the IDE, it's ALPHA_8
FileOutputStream out = new FileOutputStream(new File(appDir, "testMask16x16.png"));
boolean result = alpha.compress(Bitmap.CompressFormat.PNG, 100, out);
// result is false, i.e. the compression fails.
out.flush();
out.close();
更新:
我试图直接使用openCV保存矩阵:
Imgcodecs.imwrite(new File(appDir, "testMask16x16.png").toString(), dst);
它确实保存了它,但是在大小上似乎与保存ARGB_8888格式相同,所以我猜它是ARGB_8888格式。 :-(