gpt4 book ai didi

java - 白色和黑色图像到二维数组以及经过一些操作后二维数组到Java中的图像

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:27 26 4
gpt4 key购买 nike

我想将图像转换为二维数组,对其进行一些操作,然后再次将更改后的二维数组转换为图像。我花了很多时间寻找解决方案,但其中任何一个都是正确的。对于这个问题,网上没有很好的答案。请告诉我我错了。

最佳答案

package game;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
*
* @author rafal
*/
public class Game {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("/home/rafal/Pulpit/obraz.png"));
byte[][] pixels = new byte[image.getWidth()][];

for (int x = 0; x < image.getWidth(); x++) {
pixels[x] = new byte[image.getHeight()];

for (int y = 0; y < image.getHeight(); y++) {
pixels[x][y] = (byte) (image.getRGB(x, y) == 0xFFFFFFFF ? 0 : 1);
}
}

for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++)
{
if(pixels[x][y] == 1);
pixels[x][y] = 0;
}

}
/*
* Create a new 1-dimensional byte array which will hold the result
* Set its size to the item count in the pixelData array
*/
byte[] oneDimArray = new byte[pixels.length * pixels[0].length];
byte[][] twoDimArray = new byte[ image.getWidth()][ image.getHeight()];
/*
* Loop through the "horizontal" row in the pixelData array
*/
for(int x = 0; x < pixels.length; x++) {
/*
* Loop through each item in the current vertical row
*/
for(int y = 0; y < pixels[x].length; y++) {

/*
* Set each item in the 1-dimensional array to the corresponding
* item in the 2-dimensional array
*/
oneDimArray[x + y * pixels.length] = twoDimArray[x][y];
}
}

ByteArrayInputStream byteIn = new ByteArrayInputStream(oneDimArray);
BufferedImage finalImage = ImageIO.read(byteIn);

ImageIO.write(finalImage, "png",new File("/home/rafal/Pulpit/obraz2.png"));


}

}

我想将所有黑色像素更改为白色像素。但一般方法都是错误的。

关于java - 白色和黑色图像到二维数组以及经过一些操作后二维数组到Java中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37283227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com