我如何可以加载一个位图图像和操纵单个像素? [英] How can I load a bitmap image and manipulate individual pixels?

查看:141
本文介绍了我如何可以加载一个位图图像和操纵单个像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件中加载一个大的位图图像,运行操纵单个像素的函数,然后再保存该位图。

I want to load a single large bitmap image from a file, run a function that manipulates individual pixels and then re save the bitmap.

文件格式可以是PNG或BMP和操纵功能简单的东西,如:

File formats can be either PNG or BMP, and the manipulating function is something simple such as:

if r=200,g=200,b=200 then +20 on all values, else -100 on all values

诀窍是能够加载一个位图,并能够逐行读取每个像素行

The trick is being able to load a bitmap and being able to read each pixel line by line

有没有在Java标准库机械,可以处理这个I / O?

Is there standard library machinery in Java that can handle this I/O?

(位图将需要几百万像素,我需要能够处理百万像素)

(The bitmap will need to be several megapixels, I need to be able to handle millions of pixels)

推荐答案

感谢MadProgrammer我有一个答案:

Thanks to MadProgrammer I have an answer:

package image_test;

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

public class Image_test {

    public static void main(String[] args) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("test.bmp"));
        } catch (IOException e) {

        }
        int height = img.getHeight();
        int width = img.getWidth();

        int amountPixel = 0;
        int amountBlackPixel = 0;

        int rgb;
        int red;
        int green;
        int blue;

        double percentPixel = 0;

        System.out.println(height  + "  " +  width + " " + img.getRGB(30, 30));

        for (int h = 1; h<height; h++)
        {
            for (int w = 1; w<width; w++)
            {
                amountPixel++;

                rgb = img.getRGB(w, h);
                red = (rgb >> 16 ) & 0x000000FF;
                green = (rgb >> 8 ) & 0x000000FF;
                blue = (rgb) & 0x000000FF;

                if (red == 0 && green == 0 && blue == 0)
                {
                    amountBlackPixel ++;
                }
            }
        }
        percentPixel = (double)amountBlackPixel / (double)amountPixel;

        System.out.println("amount pixel: "+amountPixel);
        System.out.println("amount black pixel: "+amountBlackPixel);
        System.out.println("amount pixel black percent: "+percentPixel);
    }
}

这篇关于我如何可以加载一个位图图像和操纵单个像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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