java - 在java中逐像素绘制图像

标签 java image

我没有接触过图像处理。 我想在JAVA中读取.jpeg图像文件并根据颜色值在 Canvas 上绘制像素。 即先绘制所有黑色像素,然后绘制所有灰色像素,依此类推......最后绘制白色像素。

我还想在绘制的每个像素之间引入一个非常小的间隙,以便我可以看到图像是如何绘制的。

如有任何帮助,我们将不胜感激。

最佳答案

这是一个简短的说明示例,可以帮助您入门。此代码分解图像中的 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;
}

关于java - 在java中逐像素绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15297504/

相关文章:

Android - 如何将图片从 webview.capturePicture() 转换为 byte[] 并返回位图

css - Jssor slider 100% 宽度(模糊图像)

java - XStream 可序列化对象

java - Scala 对象在 Java 中的等价物是什么?

java - Java类加载是如何工作的

java - 是否有用于 Java 的复杂文件系统监视器,它是免费软件还是开源的?

html - IMG 标签上的奇怪边框

java - 如何使用java在谷歌应用程序中写入csv文件

java - 将 (1024x768 32bpp) 图像尺寸减小到 30-50 k?

css - 由文本表示的图像 - 它叫什么?