`
香煎马鲛鱼
  • 浏览: 108811 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

BMP解码器的练习

    博客分类:
  • java
阅读更多

这是一个BMP解码器的练习,实现8位,24位真彩的解码,放在这里做个记录。希望对各位有用:(分为四个文件,大家可以下载压缩文件,不需要Ctrl+A

package text;

 

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Menu;

import java.awt.MenuBar;

import java.awt.MenuItem;

 

import javax.swing.JFrame;

 

public class BMPopener extends JFrame{

      private Graphics g;

      public MenuBar creatMenuBar(){

           MenuBar mB = new MenuBar();

           //定义一个菜单

          Menu mn1 = new Menu("文件");

          MenuItem mn1_I2 = new MenuItem("打开");

          ButtonListener bL = new ButtonListener();

          mn1_I2.addActionListener(bL);

          mn1.add(mn1_I2);

          mB.add(mn1);

           return mB;

      }

      public void showUI(){

           this.setTitle("BMP打开器");

           this.setSize(1024,768);

           FlowLayout fl = new FlowLayout();

           this.setLayout(fl);

           MenuBar mB = this.creatMenuBar();

           this.setMenuBar(mB);

           this.setResizable(false);

           this.setDefaultCloseOperation(3);

           this.getContentPane().setBackground(Color.WHITE);

           this.setVisible(true);

           g = this.getGraphics();

           g.setFont(new Font("黑体",12,48));

           g.setColor(Color.red);

           g.drawString("欢迎使用", this.getHeight()/2-100,this.getWidth()/2);

      }

      public void paint(){

           super.paint(g);

           g.drawString("欢迎使用", this.getHeight()/2-100,this.getWidth()/2);

      }

    public static void main(String args[]){

    BMPopener bOpPopener = new BMPopener();

    bOpPopener.showUI();

    }

}

 

package text;

 

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

 

import javax.swing.JFileChooser;

 

public class ButtonListener implements ActionListener{

 

      JFileChooser fileChooser = new JFileChooser();

      File file;

      @Override

      public void actionPerformed(ActionEvent e) {

           // TODO Auto-generated method stub

           fileChooser.showOpenDialog(null);

           file= fileChooser.getSelectedFile();

           String path = file.getAbsolutePath();

           System.out.print(path);

           JiemaBMP jBmp = new JiemaBMP(path);

           jBmp.showJiema();

      }

}

 

 

package text;

 

import java.awt.Color;

import java.awt.Graphics;

import java.io.DataInputStream;

import java.io.EOFException;

import java.io.FileInputStream;

import java.io.IOException;

 

import javax.swing.JFrame;

 

public class JiemaBMP extends JFrame{

 

      private String pathString;

      private Graphics g;

      private UserColor[][] userColors;

      private int unith = 23;

      private UserColor[] userTiaoseban;

      public  JiemaBMP(String path) {

           this.pathString =path;

      }

      public void showJiema() {

           // TODO Auto-generated method stub

           try{

                 FileInputStream fis = new FileInputStream(pathString);

                 DataInputStream dis = new DataInputStream(fis);

                 if(dis.read()!= 'B'){

                      throw new IOException("Not a .BMP file");    

                 }

                 if(dis.read()!='M'){

                      throw new IOException("Not a .BMP file");

                 }

                 //得到总大小

                 int fileSize = readInt(dis);

                 System.out.println("文件大小:"+fileSize);

                 System.out.println(readUsignedShort(dis));

                 System.out.println(readUsignedShort(dis));

                 //读取偏移量

                 int bitmapOffset = readInt(dis);

                 //读取当前结构体的大小

                 System.out.println("bitmapOffset="+bitmapOffset);

                 int bitmapInfoSize = readInt(dis);

                 System.out.println("bitmapInfoSize"+bitmapInfoSize);

                 //读取宽高

                 int width = readInt(dis);

                 int height = readInt(dis);

                 System.out.println("width:"+width+"height:"+height);

                 //Plants总是1

                 System.out.println(readUsignedShort(dis));

                 //读位数

                 int bitCount = readUsignedShort(dis);

                 System.out.println("bitCount"+bitCount);

                 //读取压缩方式

                 int compressionType = readInt(dis);

                 System.out.println("CompressionType:"+compressionType);

                 //读取图片大小

               int imageSize = readInt(dis);

               //读分辨率

               readInt(dis);

               readInt(dis);

                //读位图使用的颜色数

               int colorsUsed = readInt(dis);

               int colorsImportant = readInt(dis);

               System.out.println("[colorused]= "+colorsUsed);

               //  Read the pixels from the stream based on the compression type

               if(compressionType ==0){

                     if(bitCount == 24||bitCount == 4){

                          readRGB24(width, height, dis);

                     }

                     if(bitCount == 8||bitCount == 1){

                          readRGB8(width, height, dis);

                     }

//                   if(bitCount == 4){

//                        readRGB4(width, height, dis);

//                   }

//                   if(bitCount == 1){

//                        readRGB1(width, height, dis);

//                   }

               }

              

           }catch(Exception e){

                

           }

      }

      public void readRGB1(int width,int height,DataInputStream dis) throws IOException{

     

      }

      public void readRGB4(int width,int height,DataInputStream dis) throws IOException{

          

      }

      public void readRGB8(int width,int height,DataInputStream dis)throws IOException{

           this.setTitle(pathString);

           //设置一个大小

           this.setSize(width,height+unith);

           this.setResizable(false);

           this.setVisible(true);

           g = this.getGraphics();

           userTiaoseban = new UserColor[256];

           userColors = new UserColor[width][height];

           for(int i = 0;i<256;i++){

                 int blue = dis.read();

                 int green = dis.read();

                 int red = dis.read();

                 dis.read();

                 userTiaoseban[i] = new UserColor(red, green, blue);

                 System.out.println("加了="+i);

           }

           //System.out.println(userTiaoseban[217].red+userTiaoseban[217].green);

           //按行读取如果H,W为正则到这来

           for(int h = height-1;h>=0;h--){

                 for(int w = 0;w<width;w++){

          //  Read in the red, green, and blue components

                 int num = dis.read();

                  

                    userColors[w][h] = userTiaoseban[num];

                    if((userColors[w][h].red!=255)||(userColors[w][h].green!=255)||(userColors[w][h].blue!= 255)){

                         g.setColor(new Color(userColors[w][h].red,userColors[w][h].green,userColors[w][h].blue));

                         g.fillOval(w, h+unith, 1, 1);

                        

                    }

                 }

           }

      }

      public void readRGB24(int width,int height,DataInputStream dis)throws IOException{

           this.setTitle(pathString);

           //设置一个大小

           this.setSize(width,height+unith);

           this.setResizable(false);

           this.setVisible(true);

           g = this.getGraphics();

           userColors = new UserColor[width][height];

           //System.out.println(userTiaoseban[217].red+userTiaoseban[217].green);

           //按行读取如果H,W为正则到这来

           for(int h = height-1;h>=0;h--){

                 for(int w = 0;w<width;w++){

          //  Read in the red, green, and blue components

                      int blue = dis.read();

                      int green = dis.read();

                      int red = dis.read();

                      userColors[w][h] = new UserColor(red, green, blue);

                    if((userColors[w][h].red!=255)||(userColors[w][h].green!=255)||(userColors[w][h].blue!= 255)){

                         g.setColor(new Color(userColors[w][h].red,userColors[w][h].green,userColors[w][h].blue));

                         g.fillOval(w, h+unith, 1, 1);

                        

                    }

                 }

            }

      }

      //构造一个整数的方法

      public final int readInt(DataInputStream dis) throws IOException{

           int ch1 = dis.read();

           int ch2 = dis.read();

           int ch3 = dis.read();

           int ch4 = dis.read();

           if((ch1|ch2|ch3|ch4)<0)

                 throw new IOException();

           return ((ch4<<24)+(ch3<<16)+(ch2<<8)+(ch1<<0));

      }

    //构造一个读2个字节整数的方法

      public final int readUsignedShort(DataInputStream dis) throws IOException{

           int ch1 = dis.read();

           int ch2 = dis.read();

           if((ch1|ch2)<0)

                 throw new EOFException();

           return(ch2<<8)+(ch1<<0);

      }

      public final int disHH(int num){

           return (num&8)>>3;

      }

      public final int disHL(int num){

           return (num&4)>>2;

      }

      public final int disLH(int num){

           return (num&2)>>1;

      }

      public final int disLL(int num){

           return (num&1);

      }

    /*

     * 定义一个将高位输出的方法(针对RGB4)

     */

      public final int disH(int num){

           return num>>4;

      }

    /*

     * 定义一个将低位输出的方法(针对RGB4)

     */

      public final int disL(int num){

           int num1 = 15;

           return num&num1;

      }

      //重写重绘方法

      public void paint(Graphics g){

           super.paint(g);

           for(int h = userColors[0].length-1;h>=0;h--){

                 for(int w=0;w<userColors.length;w++){

                      if((userColors[w][h].red!=255)||(userColors[w][h].green!=255)||userColors[w][h].blue!=255){

                            g.setColor(new Color(userColors[w][h].red,userColors[w][h].green,userColors[w][h].blue));

                            g.fillOval(w, h+unith, 1, 1);

                      }

                 }

           }

      }

}

 

package text;

 

publicclass UserColor {

 

   publicintred;

   publicintgreen;

   publicintblue;

   public UserColor(int red,int green,int blue){

     this.red = red;

     this.green = green;

     this.blue = blue;

   }

}

 

分享到:
评论

相关推荐

    JAVA BMP解码 超详细解释

    **JAVA BMP解码详解** BMP(Bitmap)是Windows操作系统中的一种位图图像文件格式,它存储了图像的像素数据和颜色信息。在JAVA中,处理BMP图像时,我们需要理解其内部结构并进行解码,以便正确地读取和显示这些图像...

    bmp解码C++

    bmp解码的C++库,有修改和读取某一坐标的功能,初始化 new Bmp(path);

    bmp文件解码的研究与实现

    ### BMP文件解码的研究与实现 #### 一、引言 随着计算机图形学技术的不断发展,数字图像在各个领域中的应用越来越广泛。其中,BMP(Bitmap)文件格式作为一种常见的图像存储格式,在Windows环境中得到了广泛应用。...

    BMP.zip_BMP图片解码_BMP格式编解码_BMP解码_FAT unicode BMP_显示.bmp图片

    本篇文章将详细讲解如何解码BMP(Bitmap)格式的图片,以及涉及到的相关技术,包括BMP格式编解码、FAT文件系统中的Unicode支持以及如何在屏幕上显示.bmp图片。 首先,我们来理解BMP图片格式。BMP是一种无损的位图...

    stm32+ucos+ucgui+fatfs+bmp解码

    在stm32上移植ucos、ucgui、fatfs和实现bmp解码,ui没有完全实现完,但是ucos、ucgui、fatfs这部分是完成了的,因为只是测试软件、所有ui都是随便做的没有章法,有兴趣的可以拿去自己添加修改, cpu:stm32f103zet6 ...

    UCOS + UCGUI + ZLGFS + BMP解码

    在这个项目中,我们关注的是在TQ2440开发板上实现UCOS、UCGUI、ZLGFS以及BMP文件解码的功能。这四个关键组件的集成将为开发者提供一个强大的工具集,便于开发复杂的嵌入式应用。 首先,UCOS(uC/OS)是一种实时操作...

    bmp-js:用于node.js的纯JavaScript BMP编码器和解码器

    适用于node.js的纯JavaScript Bmp编码器和解码器 支持所有位解码(1、4、8、16、24、32)和24位编码。 安装 npm install bmp-js 如何使用 解码BMP var bmp = require ( "bmp-js" ) ; var bmpBuffer = fs . ...

    图片编解码技术JPEG解码GIF文件结构与解码器TjpgDec技术BMP图片文件详解文档资料.zip

    图片编解码技术JPEG解码GIF文件结构与解码器TjpgDec技术BMP图片文件详解文档资料: BMP图片文件详解.pdf E文JPEG编解码介绍.pdf GIF Decoder.pdf GIF图形文件格式文档.doc GIF文件格式详解.doc GIF文件结构与解码器....

    如何获取图像解码器信息实例C#.net源代码编写

    本实例将深入探讨如何获取图像解码器信息,这对于开发者来说是至关重要的,因为不同的解码器对应于不同格式的图像文件,如JPEG、PNG、BMP等。解码器信息可以帮助我们了解系统能够支持哪些图像格式,并且可以控制如何...

    stm32 fatfs文件系统+(JPEG、BMP解码)

    5. **JPEG和BMP解码**:STM32上实现JPEG和BMP图像文件的解码是图形用户界面(GUI)和多媒体应用的重要部分。JPEG是一种有损压缩格式,适合存储照片;BMP是无损的位图格式,常用于简单的图形或图标。 6. **JPEG解码*...

    BMP图片的解码

    给一个BMP的文件路径,返回一个解码的数组,可以在framebuffer显示这张图片,颜色的位深度为16位,适合在2440的LCD上面显示

    用c语言实现bmp编解码

    c语言 bmp图像格式编解码 获取bmp图像信息头大小 能获得RGB三分量的数据 获得图像大小

    基于IImgCtx的图像解码器.docx

    常见的解码库如Independent JPEG Group的JPEG解码器、libpng的PNG解码器、libtiff的TIFF解码器以及giflib的GIF解码器虽然提供了丰富的功能,但它们在使用时往往存在一些不便之处,例如编译配置复杂、代码调试困难...

    二维码编码解码器

    二维码编码解码器,支持对jpg、bmp、png等图片的解码操作; Java语言开发;

    nimBMP:BMP编码器和解码器

    用Nim编写的BMP编码器和解码器 支持的标准颜色模式: 1位 4位未压缩和压缩 8位未压缩和压缩 16位带色罩 24位 32位带色罩 支持倒置(自上而下)和非倒置模式 支持的颜色转换: 32/24位至1,4,8,24位 自动调色板...

    jpeg解码,将jpeg解码成bmp格式

    本主题聚焦于如何通过编程将JPEG图像解码为BMP格式,这对于图像处理和显示是常见的需求。 解码JPEG文件涉及理解其内部编码机制。JPEG采用基于离散余弦变换(DCT)的压缩方法,它将图像数据转换为频率分量,并对高频...

    BMP图片阅读器

    BMP图片阅读器是一款专为查看和分析BMP位图文件设计的应用程序,它提供了一个直观且功能丰富的平台,让用户能够轻松获取BMP图像的详细信息,包括每个像素的精确值。在图像处理领域,理解并操作BMP格式的图片是至关...

Global site tag (gtag.js) - Google Analytics