📄 decoder.java
字号:
package wbmp;import java.io.*;import java.util.*;/** * Decoder for WBMP files * Copyright (c) 2003 * @author Mark Busman * @version 1.0 * * For License and contact information see WBMPEditor.java */public class Decoder { private boolean status = false; // returns status private int theMatrix[][]; private int w, h; /** Constructor - creates a new Decoder object. @param String fileName */ public Decoder(String fileName) { String hexString = ReadFile(fileName).substring(6); // read the file and dump the firat two 0's w = HextoInt(hexString.substring(0, 2)); h = HextoInt(hexString.substring(3, 5)); hexString = hexString.substring(6); // leave only the data int data[] = getData(hexString); setFinalMatrix(data); status = true; // properly decoded and opened; } /** Gets the status this object exited with. @return boolean Status - true means OK, false means something went wrong. */ public boolean getStatus() { return status; } /** Gets the decoded ColorMatrix. @return int[][] ColorMatrix */ public int[][] getMatrix() { return theMatrix; } /** Get the width of the graphic. @return int Width */ public int getWidth() { return w; } /** Get the height of the graphic. @return int Height */ public int getHeight() { return h; } /** Converts the hex values into binary values and returns an array of binary data. @param String hexString @return int[] data - an array of integers with the value of 1 or 0. */ private int[] getData(String hexString) { StringTokenizer st = new StringTokenizer(hexString, " "); int counter = 0; // stores number of bytes to the hex string int data[] = new int[1]; String fullBinaryString = ""; // Get the number of bytes in the hex string while (st.hasMoreTokens()) { counter++; st.nextToken(); } data = new int[counter * 8]; // reset the tokenizer st = new StringTokenizer(hexString, " "); // convert the hex string to a binary string while (st.hasMoreTokens()) { int intValue = HextoInt(st.nextToken()); String binaryString = Integer.toBinaryString(intValue); if (binaryString.length() < 8) { int rem = 8 - binaryString.length(); for (int x = 0; x < rem; x++) binaryString = "0" + binaryString; } fullBinaryString = fullBinaryString + binaryString; } // Populate data array with the right values for (int x = 0; x < counter * 8; x++) { String temp = fullBinaryString.substring(x, x + 1); if (temp.equals("0")) data[x] = 0; else data[x] = 1; } return data; } /** Finalizes the ColorMatrix, converts the Data array into a 2D Matrix and drops verbose boolean information. @param int[] Data */ private void setFinalMatrix(int[] data) { int bytes = w / 8; // bytes to a line int rem = w % 8; // remainder from devision // Determine how many bytes to a line if (w < 8) bytes = 1; else { if (rem > 0) bytes++; } // create the full-blown matrix int tempMatrix[][] = new int[bytes * 8][h]; // initialize the temporary matrix int index = 0; for (int r = 0; r < h; r++) { for (int c = 0; c < bytes * 8; c++) { tempMatrix[c][r] = data[index]; index++; } } // finalize the matrix theMatrix = new int[w][h]; for (int r = 0; r < h; r++) { for (int c = 0; c < w; c++) { theMatrix[c][r] = tempMatrix[c][r]; } } } /** Reads in data in bytes and then converts it to hex values. @param String fileName @return hexString */ private String ReadFile(String fileName) { FileInputStream fis = null; String str = ""; String tmp = ""; char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; byte b; try { fis = new FileInputStream (fileName); int size = fis.available (); byte[] bytes = new byte [size]; fis.read (bytes); fis.close (); for (int x = 0; x < size; x++) { b = bytes[x]; char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] }; tmp = new String(array); str = str + tmp + " "; } } catch (IOException e) { status = false; // error occured } return str; } /** Used to get the integer(decimal) equivelants of the hex strings. @param String hexValue @return int intValue */ private int HextoInt(String hexValue) { String hexDigit = "0123456789ABCDEF"; int HextoIntValue = (hexDigit.indexOf(hexValue.substring(0,1)) * 16) + hexDigit.indexOf(hexValue.substring(1,2)); return HextoIntValue; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -