问题描述
我有将其过滤的图像的值加倍的一个二维阵列。我想这个数组转换回的BufferedImage
。这怎么可能投一个双击[] []
到的BufferedImage
?
I've got a two-dimensional array of doubles which is filtered values of an image. I want to convert this array back to a BufferedImage
. How is it possible to cast an double[][]
to a BufferedImage
?
BufferedImage b = new BufferedImage(arr.length, arr[0].length, 3);
Graphics c = b.getGraphics();
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
for(int i=0; i< arr.length; i++){
for (int j=0; j<arr[0].length; j++){
c.drawString(String.valueOf(arr[i][j]), i, j);
writer.print(arr[i][j]+" \t");
}
writer.println();
}
ImageIO.write(b, "jpg", new File("CustomImage.jpg"));
System.out.println("end");
当我绘制文件name.txt在MATLAB与imshow我可以看到我过滤的图像。然而,CustomImage.jpg只包含一种颜色。任何想法,为什么?
When I am plot the file-name.txt in matlab with imshow I can see my filtered image. However the CustomImage.jpg contains just one color. Any idea why?
与c.drawString(将String.valueOf(ARR [I] [J]),I,J)的结果:
THe result with c.drawString(String.valueOf(arr[i][j]), i, j):
c.drawString(将String.valueOf(ARR [I] [J]),0 +(I * 10),0 +(J * 10)):
c.drawString(String.valueOf(arr[i][j]), 0+(i*10), 0+(j*10)):
Matlab的PLOR阵列和第二初始灰色缩放图像的ARR第一双的双:
Matlab plor the double of arr first the double of arrays and second the initial gray scaled image:
推荐答案
您code
BufferedImage b = new BufferedImage(arr.length, arr[0].length, 3);
Graphics c = b.getGraphics();
for(int i = 0; i<arr.length; i++) {
for(int j = 0; j<arr[0].length; j++) {
c.drawString(String.valueOf(arr[i][j]), 0+(i*10), 0+(i*10));
}
}
ImageIO.write(b, "Doublearray", new File("Doublearray.jpg"));
System.out.println("end");
重构后
int xLenght = arr.length;
int yLength = arr[0].length;
BufferedImage b = new BufferedImage(xLenght, yLength, 3);
for(int x = 0; x < xLenght; x++) {
for(int y = 0; y < yLength; y++) {
int rgb = (int)arr[x][y]<<16 | (int)arr[x][y] << 8 | (int)arr[x][y]
b.setRGB(x, y, rgb);
}
}
ImageIO.write(b, "Doublearray", new File("Doublearray.jpg"));
System.out.println("end");
这篇关于双打转换一个二维数组一个BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!