日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java bmp_JAVA实现对BMP图片的读取

發布時間:2025/3/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java bmp_JAVA实现对BMP图片的读取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

BMP圖片格式,是windows自帶的一個圖片格式,(*bmp),在windows的系統下都支持這種格式,bmp格式與設備無關的位圖(DIB)格式,BMP簡稱位圖,BMP的原始數據是沒有經過壓縮處理的 ?占用的空間比其它格式的圖片要大

BMP由四部分組成 ,位圖文件頭 , 位圖信息頭 , ?顏色 , 圖像數據區

BMP圖片是三個字節為一個顏色保存,將字節拼接為int需要使用位移來做

位圖文件頭 (12個字節):

0000-0001:文件標識,為字母ASCII碼“BM”,42 4D。

0002-0005:整個文件大小,單位字節。

0006-0009:這個位置的數字是被微軟保留的

000A-000D:記錄圖像數據區的起始位置。從文件開始到位圖數據(bitmap data)之間的偏移量。

位圖信息頭()

000E-0011:圖像描述信息塊的大小

0012-0015:圖像寬度。以像素為單位。

0016-0019:圖像高度。以像素為單位。

001A-001B:圖像的plane總數(恒為1)。

001C-001D:記錄像素,也就是顏色。1 - Monochrome bitmap,4 - 16 color bitmap,8 - 256 color bitmap,F - 16位位圖,18 - 24bit (true color) bitmap,20 - 32位位圖。

001E-0021:數據壓縮方式(數值位0:不壓縮;1:8位壓縮;2:4位壓縮;3:Bitfields壓縮)。

0022-0025:圖像區數據的大小。單位字節,該數必須是4的倍數。

0026-0029:水平每米有多少像素,在設備無關位圖(.DIB)中,每字節以00H填寫。

002A-002D:垂直每米有多少像素,在設備無關位圖(.DIB)中,每字節以00H填寫。

002E-0031:此圖像所用的顏色數。

0032-0035:指定重要的顏色數。當該域的值等于顏色數時(或者等于0時),表示所有顏色都一樣重要。

顏色

每一個字節表示一個顏色 r 0~255 g??0~255 ?b?0~255 ?保留一位

圖像數據區

從下到上 從左往右的順序掃描

JAVA寫一個讀取BMP圖片

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import javax.swing.JFrame;

import javax.swing.JPanel;

/**

* 圖片查看器的窗口

*

* @author Administrator

*

*/

public class priUI extends JFrame {

public static void main(String[] args) {

priUI ui = new priUI();

ui.initUI();

}

public void initUI() {

this.setSize(600, 500);

this.setTitle("圖片查看器");

// 設置布局

FlowLayout layout = new FlowLayout();

this.setLayout(layout);

JPanel center = new myPanel();

center.setPreferredSize(new Dimension(400, 300));

center.setBackground(Color.WHITE);

this.add(center);

this.setDefaultCloseOperation(3);

this.setVisible(true);

}

/**

* 讀取BMP文件的方法(BMP24位)

*/

public int[][] readFile(String path) {

try {

// 創建讀取文件的字節流

FileInputStream fis = new FileInputStream(path);

BufferedInputStream bis = new BufferedInputStream(fis);

// 讀取時丟掉前面的18位,

// 讀取圖片的18~21的寬度

bis.skip(18);

byte[] b = new byte[4];

bis.read(b);

// 讀取圖片的高度22~25

byte[] b2 = new byte[4];

bis.read(b2);

// 得到圖片的高度和寬度

int width = byte2Int(b);

int heigth = byte2Int(b2);

// 使用數組保存得圖片的高度和寬度

int[][] date = new int[heigth][width];

int skipnum = 0;

if (width * 3 / 4 != 0) {

skipnum = 4 - width * 3 % 4;

}

// 讀取位圖中的數據,位圖中數據時從54位開始的,在讀取數據前要丟掉前面的數據

bis.skip(28);

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

for (int j = 0; j < date[i].length; j++) {

// bmp的圖片在window里面世3個byte為一個像素

int blue = bis.read();

int green = bis.read();

int red = bis.read();

// 創建一個Color對象,將rgb作為參數傳入其中

Color c = new Color(red, green, blue);

// Color c = new Color(blue,green,red);

// 將得到的像素保存到date數組中

date[i][j] = c.getRGB();

}

// 如果補0的個數不為0,則需要跳過這些補上的0

if (skipnum != 0) {

bis.skip(skipnum);

}

}

return date;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

// 將四個byte拼接成一個int

public int byte2Int(byte[] by) {

int t1 = by[3] & 0xff;

int t2 = by[2] & 0xff;

int t3 = by[1] & 0xff;

int t4 = by[0] & 0xff;

int num = t1 << 24 | t2 << 16 | t3 << 8 | t4;

return num;

}

class myPanel extends JPanel {

public void paint(Graphics g) {

super.paint(g);

// 讀取數據

int[][] date = readFile("C:\\Users\\Administrator\\Desktop\\meinv.bmp");

// 判斷是否存在

if (date != null) {

// this.setPreferredSize(new

// Dimension(date[0].length,date.length));

this.setPreferredSize(new Dimension(date[0].length, date.length));

// 遍歷

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

for (int j = 0; j < date[i].length; j++) {

Color c = new Color(date[i][j]);

g.setColor(c);

g.drawLine(j, date.length - i, j, date.length - i);

}

}

}

}

}

}

已有 0 人發表留言,猛擊->> 這里<

ITeye推薦

總結

以上是生活随笔為你收集整理的java bmp_JAVA实现对BMP图片的读取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。