Java 读取图片信息(像素数)BufferedImage类 ImageIO类

package weiguoyuan.chinaunicom.cn;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ReadPicImpl implements ReadPic{
    public Coordinate readPic(String str){
        
        Coordinate coordinate =new Coordinate();//坐标对象
        int[] rgb = new int[3];
        File file = new File("str");
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        int width = bi.getWidth();
        int height = bi.getHeight();
        int CoordinateX = width%60;//x轴的单个坐标格占的像素个数
        int CoordinateY = height%80;//y轴的单个坐标格占的像素个数
        int minx = bi.getMinX();
        int miny = bi.getMinY();
        System.out.println("width=" + width + ",height=" + height + ".");
        System.out.println("minx=" + minx + ",miniy=" + miny + ".");

        for (int i = minx; i < width; i++) {
            for (int j = miny; j < height; j++) {
                // System.out.print(bi.getRGB(jw, ih));
                int pixel = bi.getRGB(i, j);
                rgb[0] = (pixel & 0xff0000) >> 16;
                rgb[1] = (pixel & 0xff00) >> 8;
                rgb[2] = (pixel & 0xff);
//                System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + ","
//                        + rgb[1] + "," + rgb[2] + ")");
                
                if(rgb[0]==5&&rgb[1]==2&&rgb[2]==60){//判断RGB值
                    //System.out.println(i+" "+j);
                    System.out.println(width%60+" " +height%80);
                    int x=i%(CoordinateX);
                    int y=j%(CoordinateY);
                    System.out.println(x+" "+y);
                    coordinate.setX(x);
                    coordinate.setY(y);
                }
            }
        }    
        return coordinate;
    }
}
package test;
import java.io.*;  
import javax.swing.*;  
public class Start  
{  
    public static void main(String args[]) throws Exception{  
          
        //源文件,必须存在,路径可选  
        File sf = new File("D://123.png");    
          
        //目的文件,因为要向其中写入,指定文件可以不存在,由程序生成  
        File df = new File("D://456.png");  
        new ReadWriteGra(sf,df);  
        new UseGra(df);  
    }  
}  
  
class ReadWriteGra   
{  
    FileInputStream in = null;  
    FileOutputStream out = null;  
    public ReadWriteGra(File sourceFile,File destFile) throws Exception{  
        byte[] buf = new byte[1024];  
        int len = 0;  
        in = new FileInputStream(sourceFile);  
        out = new FileOutputStream(destFile,true);  
        while( (len = in.read(buf)) != -1 ){  
            out.write(buf,0,len);  
        }  
        out.close();  
    }  
}  
class UseGra extends JFrame  
{  
    public UseGra(File picFile) throws Exception{  
  
        this.setVisible(true);  
        this.setResizable(false);  
        this.setLayout(null);  
        this.setBounds(600, 200, 470, 450);  
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
        JPanel p1 = (JPanel)this.getContentPane();  
        p1.setOpaque(false);  
        p1.setLayout(null);  
        InputStream is = new FileInputStream(picFile);  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        int b = 0;  
        while((b = is.read())!=-1){  
            baos.write(b);  
        }  
        ImageIcon image = new ImageIcon(baos.toByteArray());  
        JLabel background = new JLabel(image);  
        this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));  
        background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());  
       // JButton bt = new JButton("Test_Button");  
       // p1.add(bt);  
       // bt.setBounds(10,10,150,25);  
        validate();  
    }  
} 

 

posted on 2014-11-26 16:07  weiguoyuan  阅读(2743)  评论(0)    收藏  举报

导航