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

JAVA讀取二進(jìn)制文件以及畫圖教程

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

0 引言

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

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

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

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

1 讀取二進(jìn)制文件

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

1.1 DataInputStream

構(gòu)造函數(shù)為:

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

使用基礎(chǔ)類InputStream構(gòu)造DataInputStream

方法主要有,見下表

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

代碼示例如下:

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

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

1.2 BufferedInputStream

構(gòu)造函數(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ù),接下來將對(duì)數(shù)據(jù)轉(zhuǎn)換成int型。

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

代碼如下:

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

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

2 畫圖

采用Graphics2D進(jìn)行畫圖,使用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 將所有點(diǎn)連接成線

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

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 結(jié)果

4 總結(jié)

任重而道遠(yuǎn),老師還是最牛逼的!

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

相關(guān)文章

最新評(píng)論