JAVA讀取二進制文件以及畫圖教程
0 引言
最近老師讓寫一個程序,作為學習JAVA的練習。目的在于:將一個二進制文件中的數據讀取出來,其中數據包括點的位置信息和壓力值及狀態(tài)。將這些數據畫作圖像的形式展示。
本小程序分為以下幾部分:
(1)讀取二進制文件;其中需要考慮二進制文件讀出來的是十進制數,需要將二個字節(jié)合成一個short型,并轉換為int型值。
(2)畫圖;根據讀取到的點的信息,循環(huán),如果狀態(tài)是畫,則將該點與上一點相連;
1 讀取二進制文件
所有的輸入流類都是抽象類InputStream或Reader的子類。本文主要使用其中的FilterInputStream子類中的DataInputStream和BufferedInputStream這兩個子類。
1.1 DataInputStream
構造函數為:
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數組中。
1.2 BufferedInputStream
構造函數如下:
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é)數;
示例代碼如下:
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數組中將存放文件的所有信息。
1.3 處理數據
根據以上兩種方法獲取了數據,接下來將對數據轉換成int型。
由于buff數組中存放的是一個字節(jié)一個字節(jié)的,故將兩個字節(jié)組合即可。
代碼如下:
int x = (buff[0] & 0xff) | (buff[1] & 0xff) << 8; int y = (buff[2] & 0xff) | (buff[3] & 0xff) << 8;
以上是小端模式(低地址中存放的是字數據的低字節(jié),高地址存放的是字數據的高字節(jié))的轉換,大端(與小端相反)的話直接調換一下就行。
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讀取二進制文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Java中Comparable和Comparator接口的區(qū)別
這篇文章主要介紹了詳解Java中Comparable和Comparator接口的區(qū)別的相關資料,希望通過本文大家能徹底掌握這部分內容,需要的朋友可以參考下2017-09-09springboot使用RedisRepository操作數據的實現
本文主要介紹了springboot使用RedisRepository操作數據的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05關于Idea創(chuàng)建Java項目并引入lombok包的問題(lombok.jar包免費下載)
很多朋友遇到當idea創(chuàng)建java項目時,命名安裝了lombok插件卻不能使用注解,原因有兩個大家可以參考下本文,本文對每種原因分析給出了解決方案,需要的朋友參考下吧2021-06-06java HttpServletRequest和HttpServletResponse詳解
這篇文章主要介紹了java HttpServletRequest和HttpServletResponse詳解的相關資料,需要的朋友可以參考下2016-12-12