如何在Java中获取2D图像数组的值并设置值
我有Image_1.jpg。我从Image_1.jpg获得了2D数组中的图像像素值。我设置了2D数组的前8*8块值,如下所示如何在Java中获取2D图像数组的值并设置值,java,Java,我有Image_1.jpg。我从Image_1.jpg获得了2D数组中的图像像素值。我设置了2D数组的前8*8块值,如下所示 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 16
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 ,
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 ,
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 ,
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 ,
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 ,
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 ,
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 ,
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 ,
并将图像构造为Img.jpg。再次获取这个Img.jpg图像,在2D数组中读取它,然后读取前8*8个块
-16776690 , -16776690 , -16776432 , -16776176 , -16775922 , -16775927 , -16776190 , -16645120 ,
-16776944 , -16776688 , -16776432 , -16776176 , -16775922 , -16775925 , -16776188 , -16645376 ,
-16777198 , -16776940 , -16776940 , -16776684 , -16776430 , -16776434 , -16776697 , -16580096 ,
-16777196 , -16777196 , -16776939 , -16776683 , -16776428 , -16776432 , -16776695 , -16580350 ,
-16646124 , -16711660 , -16777195 , -16776937 , -16776683 , -16776686 , -16711413 , -16449532 ,
-16646126 , -16711662 , -16776940 , -16776940 , -16776940 , -16776944 , -16711413 , -16449532 ,
-16449523 , -16580594 , -16711664 , -16776944 , -16776944 , -16776947 , -16645881 , -16384000 ,
-16449529 , -16449527 , -16646133 , -16776693 , -16776695 , -16776442 , -16645632 , -16383744 ,
我希望这些值为16。我为此编写了以下代码。我应该怎么做
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testing;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/**
*
* @author pratibha
*/
public class ConstructImage{
int[][] PixelArray;
public ConstructImage(){
try{
BufferedImage bufferimage=ImageIO.read(new File("C:\\photo\\Modified\\image_1.jpg"));
int height=bufferimage.getHeight();
int width=bufferimage.getWidth();
PixelArray=new int[width][height];
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
PixelArray[i][j]=bufferimage.getRGB(i, j);
}
}
///////create Image from this PixelArray
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
PixelArray[i][j]=16;
}
}
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
System.out.print(PixelArray[i][j]+" , ");
}
System.out.println("");
}
BufferedImage bufferImage2=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
for(int x=0;x<width;x++){
for(int y=0;y<height;y++){
bufferImage2.setRGB(x, y, PixelArray[x][y]);
}
}
File outputfile = new File("C:\\photo\\Modified\\img.jpg");
ImageIO.write(bufferImage2, "jpg", outputfile);
///////////////////////////////////////////////////////////////////
BufferedImage bufferimage1=ImageIO.read(new File("C:\\photo\\Modified\\img.jpg"));
int height1=0;
height1=bufferimage1.getHeight();
int width1=0;
width1=bufferimage1.getWidth();
int[][] PixelArray1=new int[width1][height1];
for(int i=0;i<width1;i++){
for(int j=0;j<height1;j++){
PixelArray1[i][j]=bufferimage1.getRGB(i, j);
}
}
///////create Image from this PixelArray
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
System.out.print(PixelArray1[i][j]+" , ");
}
System.out.println();
}
System.out.println(Integer.toBinaryString(222));
System.out.println(Integer.signum(93226268));;
}
catch(Exception ee){
ee.printStackTrace();
}
}
public static void main(String args[]){
ConstructImage c=new ConstructImage();
}
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装测试;
导入java.awt.image.buffereImage;
导入java.io.File;
导入javax.imageio.imageio;
/**
*
*@作者普拉蒂巴
*/
公共课形象{
int[][]像素阵列;
公众形象(){
试一试{
BuffereImage bufferimage=ImageIO.read(新文件(“C:\\photo\\Modified\\image_1.jpg”);
int height=bufferimage.getHeight();
int width=bufferimage.getWidth();
PixelArray=新整数[宽度][高度];
对于(int i=0;i我不确定您是否要直接用int
设置图像的值……我想您真正想做的是将其设置为“灰度值16”。您实际要做的是将像素设置为“红0,绿0,蓝16”
首先,尝试创建一个颜色(16,16,16)
,然后对其调用getRGB()
顺便说一句,JPG是一种格式,所以在保存/加载时,您不能期望值完全匹配-您可能希望使用PNG
顺便说一句,除非您试图满足第三方库的API要求,否则我建议您继续直接访问BuffereImage
类,而不是尝试创建数组副本。未压缩的图像可能会很快变得真的大真的,并且您的内存会被针头耗尽请将内存使用率翻倍。您的问题不清楚。是否要复制图像?是否要提取子图像?是否要转换图像?是否要将其打印到控制台?实际上,我要设置图像的第一个8*8块的值,以用于街道显示