在Java中逐像素绘制图像 [英] Draw image pixel by pixel in java

查看:392
本文介绍了在Java中逐像素绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有从事图像处理. 我想在JAVA中读取.jpeg图像文件,并根据颜色值在画布上绘制像素. 也就是说,先绘制所有黑色像素,然后绘制所有灰色像素,依此类推,最后绘制白色像素.

I have not been into image processing. I want to read a .jpeg image file in JAVA and draw pixels on the canvas based on the color values. i.e. draw all black pixels first, then draw all grey pixels, and so on.....and white pixels at the end.

我还想在每个绘制的像素之间引入一个很小的间隙,以便可以看到图像的绘制方式.

I also want to introduce a very small gap between each pixel drawn so that I can see how the image is being drawn.

任何帮助将不胜感激.

推荐答案

下面是一个简短的,精简的说明示例,它将帮助您入门.此代码分解图像中的RGB值.然后,您可以对数据做任何您需要做的事情.

Here is a short, stripped down, instruction example that will get you started. This code breaks down the RGB values from the image. You then do whatever you need do with the data.

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中逐像素绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆