» 您尚未 登录   注册 | 社区服务 | 帮助 | 社区 | 无图版





Cyaline 開発者新技術BBS -> java -> JAVA的图像文件读取
 XML   RSS 2.0   WAP 

--> 本页主题: JAVA的图像文件读取 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题
ithuriel




该用户目前不在线
级别: 新手上路
精华: 0
发帖: 8
: 5
: 34 RMB
: 0
在线时间:0(小时)
注册时间:2005-09-14
最后登录:2008-04-13
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子

JAVA的图像文件读取

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;


public class BMP{
  public static float[][] Pixels;
 
  public static int constructInt(byte[] in, int offset) {
     int ret = ((int) in[offset + 3] & 0xff);
     ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
     ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
     ret = (ret << 8) | ((int) in[offset + 0] & 0xff);
     return (ret);
  }

  public static int constructInt3(byte[] in, int offset) {
    int ret = 0xff;
    ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
    ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
    ret = (ret << 8) | ((int) in[offset + 0] & 0xff);
    return (ret);
  }

  public static long constructLong(byte[] in, int offset) {
    long ret = ((long) in[offset + 7] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);
    ret |= (ret << 8) | ((long) in[offset + 0] & 0xff);
    return (ret);
  }

  public static double constructDouble(byte[] in, int offset) {
    long ret = constructLong(in, offset);
    return (Double.longBitsToDouble(ret));
  }

  public static short constructShort(byte[] in, int offset) {
    short ret = (short) ((short) in[offset + 1] & 0xff);
    ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));
    return (ret);
  }


  static class BitmapHeader {
    public int nsize;
    public int nbisize;
    public int nwidth;
    public int nheight;
    public int nplanes;
    public int nbitcount;
    public int ncompression;
    public int nsizeimage;
    public int nxpm;
    public int nypm;
    public int nclrused;
    public int nclrimp;

    public void read(FileInputStream fs) throws IOException

    {

        final int bflen = 14;
        byte bf[] = new byte[bflen];
        fs.read(bf, 0, bflen);
        final int bilen = 40;
        byte bi[] = new byte[bilen];
        fs.read(bi, 0, bilen);
        nsize = constructInt(bf, 2);
        nbisize = constructInt(bi, 2);
        nwidth = constructInt(bi, 4);
        nheight = constructInt(bi, 8);
        nplanes = constructShort(bi, 12);
        nbitcount = constructShort(bi, 14);
        ncompression = constructInt(bi, 16);
        nsizeimage = constructInt(bi, 20);
        nxpm = constructInt(bi, 24);
        nypm = constructInt(bi, 28);
        nclrused = constructInt(bi, 32);
        nclrimp = constructInt(bi, 36);
    }
  }

  public static float[][] getXY(float[][] inputz){
     
     float[][] returnF = new float[inputz.length][inputz[0].length];
     for(int i =0;i<inputz.length;i++){
         for(int z =0;z<inputz[0].length;z++){
             returnF[z] = inputz[inputz.length-1-i][z];
         }
     }
     return returnF;
     
  }
 
 
 
  public static float[][] read(FileInputStream fs)
  {
    try {
        BitmapHeader bh = new BitmapHeader();
        bh.read(fs);
       
        /**
        if (bh.nbitcount == 24)
          return (readMap24(fs, bh));
        if (bh.nbitcount == 32)
          return (readMap32(fs, bh));
        if (bh.nbitcount == 8)
          return (readMap8(fs, bh));
        **/
       
        if (bh.nbitcount == 24)
          return (getXY(readMap24(fs, bh)));
        if (bh.nbitcount == 32)
          return (getXY(readMap32(fs, bh)));
        if (bh.nbitcount == 8)
          return (getXY(readMap8(fs, bh)));
         
        fs.close();
    } catch (IOException e) {
    }
    return (null);
  }
  protected static float[][] readMap32(FileInputStream fs, BitmapHeader bh) throws IOException
  {
    int ndata[] = new int[bh.nheight * bh.nwidth];
    Pixels = new float[bh.nheight][bh.nwidth];
    byte brgb[] = new byte[bh.nwidth * 4 * bh.nheight];
    fs.read(brgb, 0, bh.nwidth * 4 * bh.nheight);
    int nindex = 0;
    for (int j = 0; j < bh.nheight; j++)
    {
        for (int i = 0; i < bh.nwidth; i++)
        {
          int i1 =ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(brgb, nindex);
          nindex += 4;
          Pixels[j]=i1&0xff;
        }
    }
      fs.close();
    return Pixels;
  }

  protected static float[][] readMap24(FileInputStream fs, BitmapHeader bh)throws IOException
  {
    int npad = (bh.nsizeimage / bh.nheight) - bh.nwidth * 3;
    int ndata[] = new int[bh.nheight * bh.nwidth];
    byte brgb[] = new byte[(bh.nwidth + npad) * 3 * bh.nheight];
    Pixels = new float[bh.nheight][bh.nwidth];
    fs.read(brgb, 0, (bh.nwidth + npad) * 3 * bh.nheight);
    int nindex = 0;
    for (int j = 0; j < bh.nheight; j++){
        for (int i = 0; i < bh.nwidth; i++){
          int i1=ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(brgb, nindex);
          nindex += 3;
          Pixels[j]=i1&0xff;
        }
        nindex += npad;
    }
      fs.close();
    return Pixels;
  }

  protected static float[][] readMap8(FileInputStream fs, BitmapHeader bh)throws IOException{
    int nNumColors = 0;
    if (bh.nclrused > 0){
        nNumColors = bh.nclrused;
    }else{
        nNumColors = (1 & 0xff) << bh.nbitcount;
    }
    if (bh.nsizeimage == 0){
        bh.nsizeimage = ((((bh.nwidth * bh.nbitcount) + 31) & ~31) >> 3);
        bh.nsizeimage *= bh.nheight;
    }
    int npalette[] = new int[nNumColors];
    byte bpalette[] = new byte[nNumColors * 4];
    fs.read(bpalette, 0, nNumColors * 4);
    int nindex8 = 0;
    for (int n = 0; n < nNumColors; n++){
        npalette[n] = constructInt3(bpalette, nindex8);
        nindex8 += 4;
    }
    int npad8 = (bh.nsizeimage / bh.nheight) - bh.nwidth;
    int ndata8[] = new int[bh.nwidth * bh.nheight];
    byte bdata[] = new byte[(bh.nwidth + npad8) * bh.nheight];
    Pixels = new float[bh.nheight][bh.nwidth];
    fs.read(bdata, 0, (bh.nwidth + npad8) * bh.nheight);
    nindex8 = 0;
    for (int j8 = 0; j8 < bh.nheight; j8++){
         for (int i8 = 0; i8 < bh.nwidth;i8++){
          int i1=ndata8[bh.nwidth * (bh.nheight - j8 - 1) + i8] =
          npalette[((int) bdata[nindex8] & 0xff)];
          nindex8++;
          Pixels[j8][i8]=i1&0xff;
        }
         nindex8 += npad8;
    }
    fs.close();
    return Pixels;
  }
 
 
  public static float[][] load(String sdir, String sfile) {
    return (load(sdir + sfile));
  }


  public static float[][] load(String sdir){
    try{
         FileInputStream fs = new FileInputStream(sdir);
        return (read(fs));
    }catch (IOException ex) {
        return (null);
    }
  }

  public static void main(String[] args) throws IOException
  {
    FileInputStream in = new FileInputStream("G:\\勉強中\\分析数据\\6MIHO KOBAYASHI.bmp");
    float[][] TheImage = read(in);
   
    for(int i =0;i<TheImage.length;i++){
         String c="";
         for(int j=0;j<TheImage[0].length;j++){
             c+=TheImage[j]+",";
         }
         System.out.println(c);
         
    }
   
  }
}




这里是BMP格式,需要注意的是,BMP有8,24,32等三种格式,需要区别对待。但是,医学方面,用的比较多的是24格式。

MAIN的编码是读出该BMP格式的辉度,有关辉度的概念,以后会开一个图像色彩的帖子慢慢的分析。

个人的代码,没什么注释,大家辛苦了。


大蟑螂
[楼 主] | Posted: 2008-04-13 06:19 顶端
ithuriel




该用户目前不在线
级别: 新手上路
精华: 0
发帖: 8
: 5
: 34 RMB
: 0
在线时间:0(小时)
注册时间:2005-09-14
最后登录:2008-04-13
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子

RAW的文件读取

package Wavelets.Pic.Input;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JFrame;

class RAW extends JComponent {
JFrame f;
Container con;

private Image img;

public RAW()
{
  setPreferredSize(new Dimension(512, 512));
  f = new JFrame();
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  con = f.getContentPane();

  try {
    int showraw[] = new int[512 * 512];
    FileInputStream fi = new FileInputStream("E:\\Documents and Settings\\蟑螂\\workspace\\Digital Image Processing\\Wavelets\\Pic\\Input\\6kobayashi512.raw");
    for (int i = 0; i < 512; i++) {
    for (int j = 0; j < 512; j++) {
      int chr = fi.read();
      if (chr == -1) break;
      showraw[i * 512 + j] = chr | chr << 8 | chr << 16 | 0xFF000000;
     
    }
    }
    fi.close();
    img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(512, 512, showraw, 0, 512));
  }
  catch (IOException y) {
    System.err.println(y.getMessage());
  }
  con.add(this);
 
  f.pack();
  f.setVisible(true);
  f.show();
}

public static void main(String args[])
{
  new RAW();
}

protected void paintComponent(Graphics g)
{
  if (isOpaque())
    g.clipRect(0, 0, getWidth(), getHeight());
 
  g.drawImage(img, 0, 0, this);
}
}


RAW是图像的基本格式之一,也是显示度最高的格式之一。

这里贴出读取代码。

不过,一般情况下,似乎PS转RAW格式有很大问题,至少我就对这个问题束手无策而不得不写了一个BMP文件的转换累直接从BMP读取辉度值(见上)

不过还是喜欢原始数据为RAW格式,因为不需要判断太多的东西,哈哈。


大蟑螂
[1 楼] | Posted: 2008-04-13 06:22 顶端

Cyaline 開発者新技術BBS -> java




Powered by PHPWind v5.0.1 Code © 2003-06 PHPWind
Total 0.112859(s) query 5, Time now is:09-10 06:15, Gzip enabled

You can contact us