因此,我尝试寻找解决方案,但找不到可以将RGBA格式转换为RGB格式的解决方案。
如果给出了从BufferedImage到BufferedImage转换的简单解决方案,那么这将是最好的,否则问题如下:
基本上,我必须将BufferedImage转换成MAT格式。它适用于JPG/JPEG图像,但不适用于PNG。用于转换的代码如下:
BufferedImage biImg = ImageIO.read(new File(imgSource));
mat = new Mat(biImg.getHeight(), biImg.getWidth(),CvType.CV_8UC3);
Imgproc.cvtColor(mat,matBGR, Imgproc.COLOR_RGBA2BGR);
byte[] data = ((DataBufferByte) biImg.getRaster().getDataBuffer()).getData();
matBGR.put(0, 0, data);对于具有RGBA值的图像,这会引发错误。因此,寻找一个解决方案。
提前谢谢。
发布于 2017-05-25 15:17:27
我找到了这样的解决方案:
BufferedImage oldRGBA= null;
try {
oldRGBA= ImageIO.read(new URL("http://yusufcakmak.com/wp-content/uploads/2015/01/java_ee.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final int width = 1200;
final int height = 800;
BufferedImage newRGB = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
newRGB .createGraphics().drawImage(oldRGBA, 0, 0, width, height, null);
try {
ImageIO.write(newRGB , "PNG", new File("your path"));
} catch (IOException e) {}因此,在这里,当我们创建新的BufferedImage时,我们可以用以下方法更改图像的类型:

RGB和PNG一起为我工作。
发布于 2017-05-25 18:58:46
public static BufferedImage toBufferedImageOfType(BufferedImage original, int type) {
if (original == null) {
throw new IllegalArgumentException("original == null");
}
if (original.getType() == type) {
return original;
}
BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), type);
Graphics2D g = image.createGraphics();
try {
g.setComposite(AlphaComposite.Src);
g.drawImage(original, 0, 0, null);
}
finally {
g.dispose();
}
return image;
}https://stackoverflow.com/questions/44182400
复制相似问题