java读取bmp图片像素数据_java 怎么得到一张bmp图片的像素数组

繁星淼淼

[code]

public static void main(String[] args) {

OutputStream output = null;

try {

// read bmp

BufferedImage img = ImageIO.read(new File("F:/temp/1.bmp"));

int imageType = img.getType();

int w = img.getWidth();

int h = img.getHeight();

int startX = 0;

int startY = 0;

int offset = 0;

int scansize = w;

// rgb的数组

int[] rgbArray = new int[offset + (h - startY) * scansize

+ (w - startX)];

img.getRGB(startX, startY, w, h, rgbArray, offset, scansize);        int x0 = w / 2;

        int y0 = h / 2;

        int rgb = rgbArray[offset + (y0 - startY) * scansize

                + (x0 - startX)];

        Color c = new Color(rgb);

        System.out.println("中间像素点的rgb:" + c);

        // create and save to bmp

        File out = new File("F:/temp/2.bmp");

        if (!out.exists())

            out.createNewFile();

        output = new FileOutputStream(out);

        BufferedImage imgOut = new BufferedImage(w, h, imageType);

        imgOut.setRGB(startX, startY, w, h, rgbArray, offset, scansize);

        ImageIO.write(imgOut, "bmp", output);

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        if (output != null)

            try {

                output.close();

            } catch (IOException e) {

            }

    }

}

[/code]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值