锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
浠ヤ笅鏄漿鎹㈢殑鍏蜂綋java瀹炵幇,宸茬粡閫氳繃鍗曞厓嫻嬭瘯,copy涔嬪悗鑳界洿鎺ヤ嬌鐢?
public class TransactionBmpTo16Bit{
聽private String resFilePath;
聽private String desFilePath;
聽private Bmp currentBmp = null;
聽
聽public TransactionBmpTo16Bit(String resFilePath,String desFilePath){
聽聽this.resFilePath = resFilePath;
聽聽this.desFilePath = desFilePath;
聽}
聽
聽/**
聽 * 鎶?4bit鐨刡mp鍥懼儚杞崲涓?6bit鐨勫浘鍍?br />聽 *
聽 * @return
聽 */
聽public boolean transacte(){
聽聽currentBmp = new Bmp();
聽聽try {
聽聽聽byte[] fileContent = currentBmp.loadBmp(resFilePath);
聽聽聽Bmp desBmp = new Bmp();
聽聽聽byte[] changeContent = this.changeContent(fileContent);
聽聽聽desBmp.setCurrent(changeContent);
聽聽聽desBmp.save(desFilePath);
聽聽} catch (IOException e) {
聽聽聽e.printStackTrace();
聽聽聽return false;
聽聽} catch (BusinessException e) {
聽聽聽e.printStackTrace();
聽聽聽return false;
聽聽}
聽聽return true;
聽}
聽
聽/**
聽 * 鑾峰緱鏂版枃浠剁殑澶у皬
聽 *
聽 * @return
聽 */
聽private int getNewFileSize(){
聽聽return聽 currentBmp.getWeight()*currentBmp.getHight()*2+currentBmp.getDataOffset();
聽}
聽
聽private byte[] changeContent(byte[] current){
聽聽
聽聽int newSize = this.getNewFileSize();
聽聽byte[] newByt = new byte[newSize];
聽聽for(int i=0;i<currentBmp.getDataOffset();i++){
聽聽聽newByt[i] = current[i];
聽聽}
聽聽/*璁劇疆鏂扮殑浣嶅浘澶у皬*/
聽聽byte[] sbt = NumberConversion.intToBytes(newSize,4);
聽聽for(int i=2;i<6;i++){
聽聽聽newByt[i]=sbt[5-i];
聽聽}
聽聽/*璁劇疆鏂扮殑鍍忕礌浣嶆暟*/
聽聽byte[] pixDigit = NumberConversion.intToBytes(16,2);
聽聽newByt[28] = pixDigit[1];
聽聽newByt[29] = pixDigit[0];
聽聽
聽聽
聽聽//TODO 鑾峰緱杞寲鍚庣殑鏁版嵁瀹炵幇
聽聽int dataSize = newSize-54;
聽聽
聽聽byte[] newData = new byte[dataSize];
聽聽
聽聽byte[][] data = this.getFiltratedArray(current);
聽聽int tt = 0;
聽聽for(int i=0;i<currentBmp.getHight();i++){
聽聽聽
聽聽聽for(int j=0;j<currentBmp.getWeight()*3;j++){
聽聽聽聽tt+=1;
聽聽聽聽if((j+1)%3==0){
聽聽聽聽聽byte n = (byte) ((((data[i][j-1])>>>5)&0x7)|(data[i][j]&0xF8));
聽聽聽聽聽byte m = (byte) (((data[i][j-2]>>>3)&0x1F)|((data[i][j-1]&0x1c)<<3));
聽聽聽聽聽int index = tt/3*2;
聽聽聽聽聽newData[index-2] = m;
聽聽聽聽聽newData[index-1] = n;
聽聽聽聽}
聽聽聽}
聽聽}
聽聽for(int i=54;i<newSize;i++){
聽聽聽newByt[i] = newData[i-54];
聽聽}
聽聽return newByt;
聽}
聽
聽/**
聽 * 榪囨護bmp琛ヤ綅鐨勬暟鎹?br />聽 * @return
聽 */
聽private byte[][] getFiltratedArray(byte[] current){
聽聽
聽聽int residue = (this.currentBmp.getWeight()*3)%4;
聽聽int skip = 0;
聽聽if(residue!=0) skip = 4-residue;
聽聽
聽聽byte[][] array = new byte[this.currentBmp.getHight()][this.currentBmp.getWeight()*3];
聽聽
聽聽int scale = this.currentBmp.getDataOffset();
聽聽
聽聽for(int i=0;i<this.currentBmp.getHight();i++){
//聽聽聽scale += i*hight;
聽聽聽for(int j=0;j<this.currentBmp.getWeight()*3;j++){
聽聽聽聽
聽聽聽聽array[i][j] = current[scale];
聽聽聽聽scale += 1;
聽聽聽}
//聽聽聽System.out.println("scale="+scale);
聽聽聽scale+=skip;
//聽聽聽System.out.println("scale1="+scale);
聽聽}
聽聽return array;
聽聽
聽}
聽
}
<!----------------------------------------------------
public class Bmp {
聽
聽protected final Log logger = LogFactory.getLog(getClass());
聽
聽public final static int BITMAPFILEHEADER_SIZE = 14;
聽
聽public final static int BITMAPINFOHEADER_SIZE = 40;
聽/*鏂囦歡澶у皬*/
聽private int size;
聽/*鏂囦歡瀹藉害*/
聽private int weight;
聽/*鏂囦歡楂樺害*/
聽private int hight;
聽/*鏁版嵁鍋忕Щ閲?/
聽private int dataOffset;
聽private byte[] current;
聽private String filePath;
聽
/*get(),set()鏂規硶鑰冭檻鍒拌妭鐪佺瘒騫?璇瘋嚜琛岃緗?/
聽public Bmp(){
聽聽
聽}
聽
聽/**
聽 * 鍔犺澆bmp鏂囦歡
聽 *
聽 * @param filePath
聽 * @return
聽 * @throws IOException
聽 * @throws BusinessException
聽 */
聽public byte[] loadBmp(String filePath) throws IOException, BusinessException{
聽聽
聽聽this.filePath = filePath;
聽聽this.setParam();
聽聽
聽聽return current;
聽}
聽
聽/**
聽 * 淇濆瓨bmp鏂囦歡
聽 *
聽 * @param desPath
聽 * @throws BusinessException
聽 * @throws IOException
聽 */
聽public void save(String desPath) throws BusinessException, IOException{
聽聽if(current==null){
聽聽聽throw new BusinessException("",null);
聽聽}
聽聽
聽聽FileOutputStream fos = new FileOutputStream(desPath);
聽聽fos.write(current,0,current.length);
聽聽fos.flush();
聽聽fos.close();
聽}
聽
聽private void setParam() throws IOException, BusinessException{
聽聽
聽聽/*鍒ゆ柇婧愭枃浠舵槸鍚︽槸bmp鏍煎紡,鍚庣紑鍙互鏄?bmp銆?dib銆?rle*/
聽聽if(!filePath.contains(".bmp")){
聽聽聽throw new BusinessException("",null);
聽聽}
聽聽
聽聽FileInputStream聽 fis = new FileInputStream(filePath);
聽聽/*bmp鏂囦歡澶村瓨鍌?/
聽聽byte[] fh = new byte[BITMAPFILEHEADER_SIZE];
聽聽fis.read(fh,0,BITMAPFILEHEADER_SIZE);
聽聽/*鏂囦歡澶翠俊鎭瓨鍌?/
聽聽byte[] hi = new byte[BITMAPINFOHEADER_SIZE];
聽聽fis.read(hi,0,BITMAPINFOHEADER_SIZE);
聽聽
聽聽/*璁劇疆鏂囦歡闀垮害*/
聽聽this.size = (((int)fh[5]&0xff)<<24)
聽聽聽聽聽聽聽 聽聽聽聽| (((int)fh[4]&0xff)<<16)
聽聽聽聽聽聽聽 聽聽聽聽聽| (((int)fh[3]&0xff)<<8)
聽聽聽聽聽聽聽 聽聽聽聽聽聽| (int)fh[2]&0xff;
聽聽/*璁劇疆鏂囦歡瀹藉害*/
聽聽this.weight = (((int)hi[7]&0xff)<<24)
聽聽聽聽聽聽聽 聽聽聽聽| (((int)hi[6]&0xff)<<16)
聽聽聽聽聽聽聽 聽聽聽聽聽| (((int)hi[5]&0xff)<<8)
聽聽聽聽聽聽聽 聽聽聽聽聽聽| (int)hi[4]&0xff;
聽聽/*璁劇疆鏂囦歡楂樺害*/
聽聽this.hight = (((int)hi[11]&0xff)<<24)
聽聽聽聽聽聽| (((int)hi[10]&0xff)<<16)
聽聽聽聽聽聽聽| (((int)hi[9]&0xff)<<8)
聽聽聽聽聽聽聽聽| (int)hi[8]&0xff;
聽聽/*璁劇疆浣嶅浘鏁版嵁闃靛垪璧峰鍋忕Щ閲?/
聽聽this.dataOffset = (((int)fh[13]&0xff)<<24)
聽聽聽聽聽聽聽聽 聽聽聽聽| (((int)fh[12]&0xff)<<16)
聽聽聽聽聽聽聽聽 聽聽聽聽聽| (((int)fh[11]&0xff)<<8)
聽聽聽聽聽聽聽聽 聽聽聽聽聽聽| (int)fh[10]&0xff;
聽
聽聽fis.close();
聽聽loadAll();
聽}
聽
聽private void loadAll() throws IOException{
聽聽
聽聽FileInputStream聽 fis = new FileInputStream(filePath);
聽聽current = new byte[size];
聽聽
聽聽fis.read(current,0,size);
聽聽
聽聽fis.close();
聽}
}
<!----------------------------------
public class NumberConversion {
聽
聽/**
聽 * 鏁村艦杞寲涓轟簩榪涘埗瀛楄妭
聽 *
聽 * @param number聽聽闇瑕佽漿鍖栫殑鏁板瓧
聽 * @param bytes聽聽聽瀛楄妭鏁?br />聽 * @return
聽 */
聽public static byte[] intToBytes(int number,int digit){
聽聽
聽聽byte[] byts = new byte[digit];
//聽聽int mask=0xff;
聽聽int basic = 8*(digit-1);
聽聽for(int i=0;i<digit;i++){
聽聽聽byts[i] = (byte)(number>>>(basic-i*8));
聽聽}
聽聽
聽聽return byts;
聽聽
聽}
聽
聽/**
聽 *
聽 * @param bytes
聽 * @return
聽 */
聽public static int bytesToInt(byte[] b){
聽聽
聽聽int mask=0xff;
聽聽int temp=0;
聽聽int res=0;
聽聽
聽聽for(int i=0;i<4;i++){
聽聽聽聽聽聽聽聽 res<<=8;
聽聽聽聽聽聽聽聽 temp=b[i]&mask;
聽聽聽聽聽聽聽聽 res|=temp;
聽聽}
聽聽return res;
聽}
}