用Java读取图像 [英] Reading an image in java

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

问题描述

您好....我想在Java中以像素RGB矩阵的形式读取现有图像时需要一点帮助.我尝试在网络上bt无法完成任何结论性的工作.

hello....i wanted a little help on how to read an pre-existing image in the form of a pixel RGB matrix in java. i tried on the net bt couldnt gt anything conclusive.

推荐答案

talisman.anant写道:
talisman.anant wrote:

我尝试在网络上进行bt测试,没有任何结论性的结果.

i tried on the net bt couldnt gt anything conclusive.



那么这一切怎么样? [ ^ ]?



What about all these[^]?


您可以从图像中获取Raster ,然后从中获取像素.

这是一个(未优化的)示例:

You can get the Raster out of an image and then get the pixels out of that.

Here''s an (unoptimized) example:

package sandbox;

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

public class Main {

    public static void main(String[] args) {
        try {
            BufferedImage image = ImageIO.read(new File("c:\\alien.jpg"));

            Raster raster = image.getRaster();
            int[][] imagePixels = new int[image.getWidth()][image.getHeight()];
            for(int x = 0; x < image.getWidth(); ++x) {
                for(int y = 0; y < image.getHeight(); ++y) {

                    int pixel = 0;
                    int[] pixelData = raster.getPixel(x, y, new int[4]);

                    for(int i = 0; i < pixelData.length; ++i) {
                        pixel |= pixelData[i] << (i * 8);
                    }
                    //System.out.println(x + ", " + y + " = " + pixel);
                }
            }

        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}



希望这会有所帮助,
弗雷德里克(Fredrik)



Hope this helps,
Fredrik


这篇关于用Java读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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