BMP 그림 프로그램 열기

4960 단어 데이터 구조swing
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.JFrame;

/**
 *   BMP       
 * 
 * @author XiongMinghua
 * 
 *            
 */
public class OpenBMP extends JFrame {
 public static void main(String[] args) {
  OpenBMP obmp = new OpenBMP();
  String path = "D:\\My Documents\\StudySample\\src\\images\\Sunset.bmp";
  obmp.openFile(path);
 }

 private void openFile(String path) {
  try {
   //        
   FileInputStream fis = new FileInputStream(path);
   //               
   BufferedInputStream bis = new BufferedInputStream(fis);
   //   BMP        
   int bflen = 14;
   byte[] bf = new byte[bflen];
   bis.read(bf, 0, bflen);//   14   BMP   

   //        
   int bilen = 40;
   byte[] bi = new byte[bilen];
   bis.read(bi, 0, bilen);//   40   BMP     

   /**
    *         :      、  、          (      )、    (     0)
    */
   image_width = changeInt(bi, 7);//     
   image_height = changeInt(bi, 11);//     
   //          ,   1(  )、4(16 )、8(256 )、24(   )  ,(29-30  )
   biBitCount = (((int) bi[15] & 0xff) << 8) | ((int) bi[14] & 0xff);

   sizeImage = changeInt(bi, 23);//      ,      (35-38  ),     sizeImage=0;
   //            ,     imageR, imageB, imageG       
   readRGB(bis);
   //    
   bis.close();
   fis.close();
   showUI(path);

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 //          
 public void readRGB(BufferedInputStream bis) {

  if (!(image_width * 3 % 4 == 0)) {
   skip_width = 4 - image_width * 3 % 4;
  }

  imageR = new int[image_height][image_width];
  imageG = new int[image_height][image_width];
  imageB = new int[image_height][image_width];
  for (int h = image_height - 1; h >= 0; h--) {
   for (int w = 0; w < image_width; w++) {
    int blue;
    try {
     blue = bis.read();//       
     int green = bis.read();//        
     int red = bis.read();//         
     imageR[h][w] = red;
     imageG[h][w] = green;
     imageB[h][w] = blue;
    } catch (IOException e) {
     e.printStackTrace();
    }

    if (w == 0) {
     //    0 
     try {
      bis.skip(skip_width);
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
  }
 }

 //        
 void showUI(String path) {
  this.setTitle(path);
  this.setSize(image_width, image_height);
  this.setDefaultCloseOperation(3);
  this.setVisible(true);
  this.getGraphics();
  repaint();//       
 }

 //     
 public void paint(java.awt.Graphics g) {
  super.paint(g);//          
  for (int h = 0; h < image_height; h++) {
   for (int w = 0; w < image_width; w++) {
    g.setColor(new java.awt.Color(imageR[h][w], imageG[h][w],
      imageB[h][w]));
    g.drawLine(w, h, w, h);
   }
  }
 }

 private int changeInt(byte[] bi, int i) {
  return (((int) bi[i] & 0xff) << 24) | (((int) bi[i - 1] & 0xff) << 16)
    | (((int) bi[i - 2] & 0xff) << 8) | (((int) bi[i - 3] & 0xff));
 }

 /**
  * 1.BMP   ( 14   )
  */
 int bfType;//       ,     ,        'B',        'M'(1-2  )
 int bfSize;//        ,      (3-6  )
 //  7-10         , Int  0
 //          ,      (11-14  )
 int bfOffBits;
 /**
  * 2.     ( 40  )
  */
 int size;//            , 40   (15-18  )
 int image_width;//      ,      (19-22  )
 int image_height;//      ,      (23-26  )
 int planes;//        ,   1(27-28  )
 int biBitCount;//          ,   1(  )、4(16 )、8(256 )、24(   )  ,(29-30  )
 int biCompression;//       ,   0(   )、1(BI_RLE8    )、2(BI_RLE4    )  。(31-34  )

 int sizeImage;//      ,      (35-38  )
 int biXPelsPerMeter;//        ,     (39-42  )
 int biYPelsPerMeter;//        ,     (43-46  )
 int biClrUsed;//                (47-50  )
 int biClrImportant;//              (51-54  )

 //    0 
 int skip_width;

 /**
  *    
  *      RGBQUAD        biBitCount   。 biBitCount   1,4,8 ,   2,16,256   ;
  *  biBitCount=24 ,      
  */
 class RGBQUAD {
  byte rgbBlue;//      (    0-255)
  byte rgbGreen;//      (    0-255)
  byte rgbRed;//      (    0-255)
  byte rgbReserved;//   ,   0
 }

 //     RGB       
 int[][] imageR, imageB, imageG;

}

좋은 웹페이지 즐겨찾기