java – 基于int数组创建一个WritableRaster
作者:互联网
我需要一个int数组并将其转换为BufferImage.我真的没有任何关于这个主题的背景知识,我从互联网上学到了所有这些,所以这就是我想要做的:
从BufferedImage(完成)创建数组,将此数组转换为IntBuffer(完成) – (稍后我需要通过IntBuffer对图像执行一些操作),将IntBuffer中更改的值放入新数组(完成),并将此数组转换为WritableRaster.
(如果在我的过程中有些不对劲,请告诉我)
这是我处理WritableRaster的行:
WritableRaster newRaster= newRaster.setPixels(0, 0, width, height, matrix);
Eclipse将此标记为错误并说”类型不匹配:无法从void转换为WritableRaster“
请帮忙!我有点迷茫.
也很抱歉英语不好.
编辑:
矩阵:
int height=img.getHeight();
int width=img.getWidth();
int[]matrix=new int[width*height];
我尝试将值插入Raster的代码部分:
BufferedImage finalImg = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
WritableRaster newRaster= (WritableRaster)finalImg.getData();
newRaster.setPixels(0, 0, width, height, matrix);
错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10769
at java.awt.image.SinglePixelPackedSampleModel.setPixels(Unknown Source)
at java.awt.image.WritableRaster.setPixels(Unknown Source)
解决方法:
您可以从int数组创建WritableRaster和/或BufferedImage,如下所示:
int w = 300;
int h = 200;
int[] matrix = new int[w * h];
// ...manipulate the matrix...
DataBufferInt buffer = new DataBufferInt(matrix, matrix.length);
int[] bandMasks = {0xFF0000, 0xFF00, 0xFF, 0xFF000000}; // ARGB (yes, ARGB, as the masks are R, G, B, A always) order
WritableRaster raster = Raster.createPackedRaster(buffer, w, h, w, bandMasks, null);
System.out.println("raster: " + raster);
ColorModel cm = ColorModel.getRGBdefault();
BufferedImage image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
System.err.println("image: " + image);
标签:raster,java,image-processing 来源: https://codeday.me/bug/20190823/1695530.html