?? encoder.java
字號:
package wbmp;import java.io.*;/** * Encodes and saves a WBMP file. * Copyright (c) 2003 * @author Mark Busman * @version 1.0 * * For License and contact information see WBMPEditor.java */public class Encoder { private int theMatrix[][]; /** Constructor - constructs a new Encoder object. Converts the 2D ColorMatrix into a 1D hexadecimal stream. @param int Width @param int Height @param int[][] ColorMatrix @param String fileName */ public Encoder(int width, int height, int matrix[][], String fName) { int bytes = width / 8; // bytes to a line int rem = width % 8; // remainder from devision // Determine how many bytes to a line if (width < 8) bytes = 1; else { if (rem > 0) bytes++; } // Create the final matrix and initialize it to 0 theMatrix = new int[bytes * 8][height]; for (int h = 0; h < height; h++) { for (int w = 0; w < bytes * 8; w ++) { theMatrix[w][h] = 0; //populate matrix with appropriate value } } // copy the old values from the original matrix into the final matrix for (int h = 0; h < height; h++) { for (int w = 0; w < width; w ++) { theMatrix[w][h] = matrix[w][h]; } } // Create the output stream, in integers int index = 0; int byteArray[] = new int[bytes * height]; for (int h = 0; h < height; h++) { for (int b = 0; b < bytes; b++) { int value = 256; int sum = 0; for (int w = b * 8; w < 8 * b + 8; w++) { value = value / 2; if (theMatrix[w][h] == 1) sum = sum + value; } byteArray[index] = sum; index++; } } WriteFile(fName, byteArray, width, height, bytes * height); } /** Writes the WBMP file to disk. @param String fileName @param int[] Data @param int Width @param int Height @param int DataSize. */ private void WriteFile(String fileName, int data[], int width, int height, int ArraySize) { FileOutputStream fos = null; try { FileOutputStream DataOut = new FileOutputStream(fileName); // write the initialization DataOut.write(0); DataOut.write(0); DataOut.write(width); DataOut.write(height); // write the data for (int c = 0; c < ArraySize; c++) DataOut.write(data[c]); DataOut.close(); } catch (IOException e) { } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -