[英]Draw image pixel by pixel in java
我还没有进入图像处理。 我想在 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.