欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA讀取二進制文件以及畫圖教程

 更新時間:2023年07月12日 09:38:43   作者:這個人太懶了  
由于項目需要,需要對二進制文件進行讀取,所以這篇文章主要給大家介紹了關于JAVA讀取二進制文件以及畫圖的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

0 引言

最近老師讓寫一個程序,作為學習JAVA的練習。目的在于:將一個二進制文件中的數(shù)據(jù)讀取出來,其中數(shù)據(jù)包括點的位置信息和壓力值及狀態(tài)。將這些數(shù)據(jù)畫作圖像的形式展示。

本小程序分為以下幾部分:

(1)讀取二進制文件;其中需要考慮二進制文件讀出來的是十進制數(shù),需要將二個字節(jié)合成一個short型,并轉(zhuǎn)換為int型值。

(2)畫圖;根據(jù)讀取到的點的信息,循環(huán),如果狀態(tài)是畫,則將該點與上一點相連;

1 讀取二進制文件

所有的輸入流類都是抽象類InputStream或Reader的子類。本文主要使用其中的FilterInputStream子類中的DataInputStream和BufferedInputStream這兩個子類。

1.1 DataInputStream

構造函數(shù)為:

DataInputStream(InputStream in)
Creates a DataInputStream that uses the specified underlying InputStream.

使用基礎類InputStream構造DataInputStream

方法主要有,見下表

本文使用readFully(byte[] b)方法讀取所有的字節(jié)信息。

代碼示例如下:

DataInputStream dis = null;
dis = new DataInputStream(new FileInputStream ("./test.txt"));
byte []b = new byte [1024];
dis.read(b);

文件的所有信息都會存儲在定義的byte數(shù)組中。

1.2 BufferedInputStream

構造函數(shù)如下:

BufferedInputStream(InputStream in)
Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
BufferedInputStream(InputStream in, int size)
Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

方法主要有,見下表

主要使用read(byte [], off, len)方法讀取文件信息。方法available()返回文件的字節(jié)數(shù);

示例代碼如下:

BUfferedInputStream bis = null;
bis = new BufferedInputStream(new fileInputStream("./test.txt"));
int len = bis.available();
byte []b = new byte[len];
bis.read(b, 0, len);

byte數(shù)組中將存放文件的所有信息。

1.3 處理數(shù)據(jù)

根據(jù)以上兩種方法獲取了數(shù)據(jù),接下來將對數(shù)據(jù)轉(zhuǎn)換成int型。

由于buff數(shù)組中存放的是一個字節(jié)一個字節(jié)的,故將兩個字節(jié)組合即可。

代碼如下:

int x = (buff[0] & 0xff) | (buff[1] & 0xff) << 8;
int y = (buff[2] & 0xff) | (buff[3] & 0xff) << 8;

以上是小端模式(低地址中存放的是字數(shù)據(jù)的低字節(jié),高地址存放的是字數(shù)據(jù)的高字節(jié))的轉(zhuǎn)換,大端(與小端相反)的話直接調(diào)換一下就行。

2 畫圖

采用Graphics2D進行畫圖,使用BufferedImage創(chuàng)建畫,并通過方法getGraphics()返回2D圖像。

 // create a BufferedImage with the size of (width, height)
        BufferedImage bufferedImage = new BufferedImage(width, height,                 
        BufferedImage.TYPE_INT_RGB);
        // to draw strokes, we need a Graphics2D - correlated with BufferedImage
        Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
        // set the background to be WHITE
        graphics2D.setBackground(Color.WHITE);
        graphics2D.clearRect(0, 0, width, height);
        // set width and color for lines
        graphics2D.setPaint(Color.BLACK);
        graphics2D.setStroke(new BasicStroke(3));

2.1 將所有點連接成線

判斷該點的狀態(tài)是否是畫,及下一個點是否是畫,然后再連線

int pos;
        boolean bDrawing = false;
        int xPrev=-1, yPrev=-1;
        for( pos = 4; pos + 7 <= nLength ; pos += 7)
        {
            byte status = buffer[pos];
            int x = ((buffer[pos+1]&0xff) | ((buffer[pos+2]&0xff) << 8)) / 10;
            int y = ((buffer[pos+3]&0xff) | ((buffer[pos+4]&0xff) << 8)) / 10;
            if( bDrawing ) {
                if(status == 0x11) {
                    graphics2D.drawLine(xPrev, yPrev, x, y);
                    xPrev = x;
                    yPrev = y;
                }
                else {
                    bDrawing = false;
                }
            }
            else {
                if(status == 0x11) {
                    bDrawing = true;
                    xPrev = x;
                    yPrev = y;
                }
                else {
                    // floating
                }
            }
        }

3 結果

4 總結

任重而道遠,老師還是最牛逼的!

到此這篇關于JAVA讀取二進制文件以及畫圖的文章就介紹到這了,更多相關JAVA讀取二進制文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論