我还没有进入图像处理。我想在 JAVA 中读取 .jpeg 图像文件并根据颜色值在画布上绘制像素。即首先绘制所有黑色像素,然后绘制所有灰色像素,依此类推......最后是白色像素。
我还想在绘制的每个像素之间引入一个非常小的间隙,以便我可以看到图像是如何绘制的。
任何帮助,将不胜感激。
这是一个简短的、精简的说明示例,可以帮助您入门。此代码分解图像中的 RGB 值。然后,您可以对数据做任何您需要做的事情。
public static BufferedImage exampleForSO(BufferedImage image) {
BufferedImage imageIn = image;
BufferedImage imageOut =
new BufferedImage(imageIn.getWidth(), imageIn.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
int width = imageIn.getWidth();
int height = imageIn.getHeight();
int[] imageInPixels = imageIn.getRGB(0, 0, width, height, null, 0, width);
int[] imageOutPixels = new int[imageInPixels.length];
for (int i = 0; i < imageInPixels.length; i++) {
int alpha = (imageInPixels[i] & 0xFF000000) >> 24;
int red = (imageInPixels[i] & 0x00FF0000) >> 16;
int green = (imageInPixels[i] & 0x0000FF00) >> 8;
int blue = (imageInPixels[i] & 0x000000FF) >> 0;
// Make any change to the colors.
if ( conditionCheckerForRedGreenAndBlue ){
// bla bla bla
} else {
// yada yada yada
}
// At last, store in output array:
imageOutPixels[i] = (alpha & 0xFF) << 24
| (red & 0xFF) << 16
| (green & 0xFF) << 8
| (blue & 0xFF);
}
imageOut.setRGB(0, 0, width, height, imageOutPixels, 0, width);
return imageOut;
}