java读取bmp(java读取文件内容并输出)

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

本篇文章给大家谈谈java读取bmp,以及java读取文件内容并输出对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

如何用java实现1bit bmp位图读取

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.SwingUtilities;

public class TestWin extends JFrame implements ActionListener {

private JLabel imgLabel = new JLabel();

public TestWin() {

try {

Image img=ImageIO.read(new File("1bit.bmp"));

imgLabel.setIcon(new ImageIcon(img));

} catch (IOException e) {

e.printStackTrace();

}

add(imgLabel);

pack();

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

@Override

public void actionPerformed(ActionEvent e) {

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new TestWin().setVisible(true);

}

});

}

}

Java如何读取BMP的每个像素点,输出到一个二维数组

楼上的基本正确,

问题一:

int[] rgb = new int[3];最好用二维数组

int[] rgb = new int[3][width*height]

问题二:

rgb[0] = (pixel 0xff0000 ) 16 ;

rgb[1] = (pixel 0xff00 ) 8 ;

rgb[2] = (pixel 0xff );

会把数组内的值覆盖,获得就是最后像素点的RGB值;

我写了一个希望可以帮助到你

package imageReadAndWrite;

import java.awt.image.BufferedImage;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageDecoder;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**

* JPG File reader/writer. Uses native com.sun libraries (which may deprecate at

* any time)

*

*

* @author PhoenixZJG

* @version 1.0

*/

public class JPGFile implements xxxFile {

private int[] ints = null;

private byte bytes[] = null; // bytes which make up binary PPM image

private double doubles[] = null;

private int[][] imageRGB = null;

private String filename = null; // filename for PPM image

private int height = 0;

private int width = 0;

/**

* Read the PPM File.

*

* @throws FileNotFoundException

* if the directory/image specified is wrong

* @throws IOException

* if there are problems reading the file.

*/

public JPGFile(String filename) throws FileNotFoundException, IOException {

this.filename = filename;

readImage();

}

/**

* Get the height of the PPM image.

*

* @return the height of the image.

*/

public int getHeight() {

return height;

}

/**

* Get the width of the PPM image.

*

* @return the width of the image.

*/

public int getWidth() {

return width;

}

/**

* Get the data as byte array. Data is of any type that has been read from

* the file (usually 8bit RGB)

*

* @return The data of the image.

*/

public byte[] getBytes() {

return bytes;

}

/**

* Get the data as double array. Data is of any type that has been read from

* the file (usually 8bit RGB put into an 64bit double)

*

* @return The data of the image.

*/

public double[] getDouble() {

return doubles;

}

/**

* Get the data as double array. Data is of any type that has been read from

* the file (usually 8bit RGB put into an 64bit double)

*

* @return The data of the image.

*/

public int[] getInt() {

return ints;

}

/**

* Get the data as integer array. Data is of any type that has been read from

* the file (usually 8bit RGB put into an 64bit double)

*

* @return The data of the image.

*/

public int[][] getImageRGB() {

return imageRGB;

}

/**

* Write to codefn/code file the codedata/code using the

* codewidth, height/code variables. Data is assumed to be 8bit RGB.

*

* @throws FileNotFoundException

* if the directory/image specified is wrong

* @throws IOException

* if there are problems reading the file.

*/

public static void writeImage(String fn, int[] data, int width, int height)

throws FileNotFoundException, IOException {

FileOutputStream fOut = new FileOutputStream(fn);

JPEGImageEncoder jpeg_encode = JPEGCodec.createJPEGEncoder(fOut);

BufferedImage image = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

image.setRGB(0, 0, width, height, data, 0, width);

jpeg_encode.encode(image);

fOut.close();

}

/**

* Read the image from the specified file.

*

* @throws FileNotFoundException

* pretty obvious

* @throws IOException

* filesystem related problems

*/

private void readImage() throws FileNotFoundException, IOException {

FileInputStream fIn = new FileInputStream(filename);

JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);

BufferedImage image = jpeg_decode.decodeAsBufferedImage();

width = image.getWidth();

height = image.getHeight();

int[] rgbdata = new int[width * height];

image.getRGB(0, 0, width, height, rgbdata, 0, width);

ints = rgbdata;

bytes = new byte[rgbdata.length];

doubles = new double[rgbdata.length];

imageRGB = new int[3][rgbdata.length];

for (int i = 0; i bytes.length; i++) {

bytes[i] = (byte) (rgbdata[i] 0xFF);

doubles[i] = (double) (rgbdata[i]);

imageRGB[0][i] = (rgbdata[i] 16711680) 16;

imageRGB[1][i] = (rgbdata[i] 65280) 8;

imageRGB[2][i] = (rgbdata[i] 255);

}

}

}

上述代码可以复制,粘贴使用,有方法的注视,getImageRGB() 就可以获得所有像素的RGB值,你就可以在其他方法里处理这个二维数组,得到你想要的平均值了,处理后的值,记得把值逆运算,再转换到Int[]里,就可以用,writeImage()输出图像了。

java数字图像处理常用算法

     前些时候做毕业设计 用java做的数字图像处理方面的东西 这方面的资料ms比较少 发点东西上来大家共享一下 主要就是些算法 有自己写的 有人家的 还有改人家的 有的算法写的不好 大家不要见笑

一 读取bmp图片数据

//  获取待检测图像  数据保存在数组 nData[] nB[]  nG[]  nR[]中

public  void getBMPImage(String source) throws Exception {                    clearNData();                        //清除数据保存区         FileInputStream fs = null;               try {            fs = new FileInputStream(source);            int bfLen = ;            byte bf[] = new byte[bfLen];            fs read(bf bfLen); // 读取 字节BMP文件头            int biLen = ;            byte bi[] = new byte[biLen];            fs read(bi biLen); // 读取 字节BMP信息头

// 源图宽度            nWidth = (((int) bi[ ] xff) )                    | (((int) bi[ ] xff) )                    | (((int) bi[ ] xff) ) | (int) bi[ ] xff;

// 源图高度            nHeight = (((int) bi[ ] xff) )                    | (((int) bi[ ] xff) )                    | (((int) bi[ ] xff) ) | (int) bi[ ] xff;

// 位数            nBitCount = (((int) bi[ ] xff) ) | (int) bi[ ] xff;

// 源图大小            int nSizeImage = (((int) bi[ ] xff) )                    | (((int) bi[ ] xff) )                    | (((int) bi[ ] xff) ) | (int) bi[ ] xff;

// 对 位BMP进行解析            if (nBitCount == ){                int nPad = (nSizeImage / nHeight) nWidth * ;                nData = new int[nHeight * nWidth];                nB=new int[nHeight * nWidth];                nR=new int[nHeight * nWidth];                nG=new int[nHeight * nWidth];                byte bRGB[] = new byte[(nWidth + nPad) * * nHeight];                fs read(bRGB (nWidth + nPad) * * nHeight);                int nIndex = ;                for (int j = ; j nHeight; j++){                    for (int i = ; i nWidth; i++) {                        nData[nWidth * (nHeight j ) + i] = ( xff)                                 | (((int) bRGB[nIndex + ] xff) )                                 | (((int) bRGB[nIndex + ] xff) )                                | (int) bRGB[nIndex] xff;                                              nB[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex] xff;                        nG[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex+ ] xff;                        nR[nWidth * (nHeight j ) + i]=(int) bRGB[nIndex+ ] xff;                        nIndex += ;                    }                    nIndex += nPad;                } //               Toolkit kit = Toolkit getDefaultToolkit(); //               image = kit createImage(new MemoryImageSource(nWidth nHeight  //                       nData nWidth));

/*               //调试数据的读取

FileWriter fw = new FileWriter( C:\\Documents and Settings\\Administrator\\My Documents\\nDataRaw txt );//创建新文件                PrintWriter out = new PrintWriter(fw);                for(int j= ;jnHeight;j++){                 for(int i= ;inWidth;i++){                  out print(( * +nData[nWidth * (nHeight j ) + i])+ _                     +nR[nWidth * (nHeight j ) + i]+ _                     +nG[nWidth * (nHeight j ) + i]+ _                     +nB[nWidth * (nHeight j ) + i]+ );                                   }                 out println( );                }                out close();*/                      }        }        catch (Exception e) {            e printStackTrace();            throw new Exception(e);        }         finally {            if (fs != null) {                fs close();            }        }     //   return image;    }

二 由r g b 获取灰度数组

    public  int[] getBrightnessData(int rData[] int gData[] int bData[]){          int brightnessData[]=new int[rData length];     if(rData length!=gData length || rData length!=bData length       || bData length!=gData length){      return brightnessData;     }     else {      for(int i= ;ibData length;i++){       double temp= *rData[i]+ *gData[i]+ *bData[i];       brightnessData[i]=(int)(temp)+((temp (int)(temp)) ? : );      }      return brightnessData;     }          } 

三 直方图均衡化

    public int [] equilibrateGray(int[] PixelsGray int width int height)     {                  int gray;         int length=PixelsGray length;         int FrequenceGray[]=new int[length];          int SumGray[]=new int[ ];          int ImageDestination[]=new int[length];         for(int i = ; i length ;i++)         {            gray=PixelsGray[i];               FrequenceGray[gray]++;         }           //    灰度均衡化          SumGray[ ]=FrequenceGray[ ];          for(int i= ;i ;i++){               SumGray[i]=SumGray[i ]+FrequenceGray[i];           }         for(int i= ;i ;i++) {               SumGray[i]=(int)(SumGray[i]* /length);           }         for(int i= ;iheight;i++)          {                for(int j= ;jwidth;j++)               {                   int k=i*width+j;                   ImageDestination[k]= xFF | ((SumGray[PixelsGray[k]]                             ) | (SumGray[PixelsGray[k]] ) | SumGray[PixelsGray[k]]);                }           }         return ImageDestination;      } 

四 laplace 阶滤波 增强边缘 图像锐化

    public int[] laplace DFileter(int []data int width int height){         int filterData[]=new int[data length];     int min= ;     int max= ;     for(int i= ;iheight;i++){      for(int j= ;jwidth;j++){       if(i== || i==height || j== || j==width )               filterData[i*width+j]=data[i*width+j];       else        filterData[i*width+j]= *data[i*width+j] data[i*width+j ] data[i*width+j+ ]                             data[(i )*width+j] data[(i )*width+j ] data[(i )*width+j+ ]                             data[(i+ )*width+j] data[(i+ )*width+j ] data[(i+ )*width+j+ ];              if(filterData[i*width+j]min)        min=filterData[i*width+j];       if(filterData[i*width+j]max)        max=filterData[i*width+j];      }       }//     System out println( max: +max);//     System out println( min: +min);          for(int i= ;iwidth*height;i++){      filterData[i]=(filterData[i] min)* /(max min);     }     return filterData;    } 

五 laplace 阶增强滤波 增强边缘 增强系数delt

    public int[] laplaceHigh DFileter(int []data int width int height double delt){          int filterData[]=new int[data length];     int min= ;     int max= ;     for(int i= ;iheight;i++){      for(int j= ;jwidth;j++){       if(i== || i==height || j== || j==width )               filterData[i*width+j]=(int)(( +delt)*data[i*width+j]);       else        filterData[i*width+j]=(int)(( +delt)*data[i*width+j] data[i*width+j ]) data[i*width+j+ ]                             data[(i )*width+j] data[(i )*width+j ] data[(i )*width+j+ ]                             data[(i+ )*width+j] data[(i+ )*width+j ] data[(i+ )*width+j+ ];              if(filterData[i*width+j]min)        min=filterData[i*width+j];       if(filterData[i*width+j]max)        max=filterData[i*width+j];      }       }     for(int i= ;iwidth*height;i++){      filterData[i]=(filterData[i] min)* /(max min);     }     return filterData;    }  六 局部阈值处理 值化

    //   局部阈值处理 值化 niblack s   method    /*原理             T(x y)=m(x y)   +   k*s(x y)            取一个宽度为w的矩形框 (x y)为这个框的中心          统计框内数据 T(x y)为阈值 m(x y)为均值 s(x y)为均方差 k为参数(推荐 )计算出t再对(x y)进行切割 /             这个算法的优点是     速度快 效果好             缺点是     niblack s   method会产生一定的噪声        */        public int[] localThresholdProcess(int []data int width int height int w int h double coefficients double gate){     int[] processData=new int[data length];     for(int i= ;idata length;i++){      processData[i]= ;     }          if(data length!=width*height)      return processData;          int wNum=width/w;     int hNum=height/h;     int delt[]=new int[w*h];          //System out println( w; +w+   h: +h+   wNum: +wNum+ hNum: +hNum);          for(int j= ;jhNum;j++){      for(int i= ;iwNum;i++){     //for(int j= ;j ;j++){     // for(int i= ;i ;i++){         for(int n= ;nh;n++)               for(int k= ;kw;k++){                delt[n*w+k]=data[(j*h+n)*width+i*w+k];                //System out print( delt[ +(n*w+k)+ ]: +delt[n*w+k]+ );               }        //System out println();        /*        for(int n= ;nh;n++)               for(int k= ;kw;k++){                System out print( data[ +((j*h+n)*width+i*w+k)+ ]: +data[(j*h+n)*width+i*w+k]+ );               }        System out println();        */        delt=thresholdProcess(delt w h coefficients gate);        for(int n= ;nh;n++)               for(int k= ;kw;k++){                processData[(j*h+n)*width+i*w+k]=delt[n*w+k];               // System out print( delt[ +(n*w+k)+ ]: +delt[n*w+k]+ );               }        //System out println();        /*        for(int n= ;nh;n++)               for(int k= ;kw;k++){                System out print( processData[ +((j*h+n)*width+i*w+k)+ ]: +processData[(j*h+n)*width+i*w+k]+ );               }        System out println();        */      }      }          return processData;    } 

七 全局阈值处理 值化

    public int[] thresholdProcess(int []data int width int height double coefficients double gate){     int [] processData=new int[data length];     if(data length!=width*height)      return processData;     else{      double sum= ;      double average= ;      double variance= ;      double threshold;            if( gate!= ){       threshold=gate;       }      else{            for(int i= ;iwidth*height;i++){            sum+=data[i];            }            average=sum/(width*height);                  for(int i= ;iwidth*height;i++){              variance+=(data[i] average)*(data[i] average);            }            variance=Math sqrt(variance);            threshold=average coefficients*variance;      }               for(int i= ;iwidth*height;i++){          if(data[i]threshold)             processData[i]= ;          else                 processData[i]= ;         }               return processData;       }    } 

八  垂直边缘检测 sobel算子

    public int[] verticleEdgeCheck(int []data int width int height int sobelCoefficients) throws Exception{     int filterData[]=new int[data length];     int min= ;     int max= ;     if(data length!=width*height)      return filterData;          try{            for(int i= ;iheight;i++){       for(int j= ;jwidth;j++){        if(i== || i== || i==height || i==height            ||j== || j== || j==width || j==width ){               filterData[i*width+j]=data[i*width+j];         }         else{          double average;            //中心的九个像素点             //average=data[i*width+j] Math sqrt( )*data[i*width+j ]+Math sqrt( )*data[i*width+j+ ]          average=data[i*width+j] sobelCoefficients*data[i*width+j ]+sobelCoefficients*data[i*width+j+ ]                         data[(i )*width+j ]+data[(i )*width+j+ ]                     data[(i+ )*width+j ]+data[(i+ )*width+j+ ];             filterData[i*width+j]=(int)(average);         }               if(filterData[i*width+j]min)         min=filterData[i*width+j];         if(filterData[i*width+j]max)         max=filterData[i*width+j];        }        }       for(int i= ;iwidth*height;i++){        filterData[i]=(filterData[i] min)* /(max min);         }          }     catch (Exception e)      {            e printStackTrace();            throw new Exception(e);        }            return filterData;    } 

九  图像平滑 * 掩模处理(平均处理) 降低噪声

lishixinzhi/Article/program/Java/hx/201311/26286

java 怎么实现读取8位的bmp图片文件

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import java.io.*;

public class Test{

public static void main(String args[]) {

int[] rgb = new int[3];

File file = new File("a.bmp");

BufferedImage bi=null;

try{

bi = ImageIO.read(file);

}catch(Exception e){

e.printStackTrace();

}

int width=bi.getWidth();

int height=bi.getHeight();

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;iwidth;i++){

for(int j=miny;jheight;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]+")");

}

}

}

}

关于java读取bmp和java读取文件内容并输出的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

发布于 2023-04-03 10:04:45
收藏
分享
海报
40
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了