posts - 28,  comments - 15,  trackbacks - 0

          關(guān)于bmp文件內(nèi)部存儲(chǔ)格式參看:http://www.yymcu.com/resource/BMP%CE%C4%BC%FE%B8%F1%CA%BD%B7%D6%CE%F6.htm
          以下是轉(zhuǎn)換的具體java實(shí)現(xiàn),已經(jīng)通過(guò)單元測(cè)試,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;
          ?}
          ?
          ?/**
          ? * 把24bit的bmp圖像轉(zhuǎn)換為16bit的圖像
          ? *
          ? * @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];
          ??}
          ??/*設(shè)置新的位圖大小*/
          ??byte[] sbt = NumberConversion.intToBytes(newSize,4);
          ??for(int i=2;i<6;i++){
          ???newByt[i]=sbt[5-i];
          ??}
          ??/*設(shè)置新的像素位數(shù)*/
          ??byte[] pixDigit = NumberConversion.intToBytes(16,2);
          ??newByt[28] = pixDigit[1];
          ??newByt[29] = pixDigit[0];
          ??
          ??
          ??//TODO 獲得轉(zhuǎn)化后的數(shù)據(jù)實(shí)現(xiàn)
          ??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;
          ?}
          ?
          ?/**
          ? * 過(guò)濾bmp補(bǔ)位的數(shù)據(jù)
          ? * @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;
          ?/*數(shù)據(jù)偏移量*/
          ?private int dataOffset;
          ?private byte[] current;
          ?private String filePath;
          ?
          /*get(),set()方法考慮到節(jié)省篇幅,請(qǐng)自行設(shè)置*/

          ?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文件頭存儲(chǔ)*/
          ??byte[] fh = new byte[BITMAPFILEHEADER_SIZE];
          ??fis.read(fh,0,BITMAPFILEHEADER_SIZE);
          ??/*文件頭信息存儲(chǔ)*/
          ??byte[] hi = new byte[BITMAPINFOHEADER_SIZE];
          ??fis.read(hi,0,BITMAPINFOHEADER_SIZE);
          ??
          ??/*設(shè)置文件長(zhǎng)度*/
          ??this.size = (((int)fh[5]&0xff)<<24)
          ??????? ????| (((int)fh[4]&0xff)<<16)
          ??????? ?????| (((int)fh[3]&0xff)<<8)
          ??????? ??????| (int)fh[2]&0xff;
          ??/*設(shè)置文件寬度*/
          ??this.weight = (((int)hi[7]&0xff)<<24)
          ??????? ????| (((int)hi[6]&0xff)<<16)
          ??????? ?????| (((int)hi[5]&0xff)<<8)
          ??????? ??????| (int)hi[4]&0xff;
          ??/*設(shè)置文件高度*/
          ??this.hight = (((int)hi[11]&0xff)<<24)
          ??????| (((int)hi[10]&0xff)<<16)
          ???????| (((int)hi[9]&0xff)<<8)
          ????????| (int)hi[8]&0xff;
          ??/*設(shè)置位圖數(shù)據(jù)陣列起始偏移量*/
          ??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 {
          ?
          ?/**
          ? * 整形轉(zhuǎn)化為二進(jìn)制字節(jié)
          ? *
          ? * @param number??需要轉(zhuǎn)化的數(shù)字
          ? * @param bytes???字節(jié)數(shù)
          ? * @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;

          ?}
          }

          posted on 2008-04-03 14:43 zhangxl 閱讀(912) 評(píng)論(0)  編輯  收藏 所屬分類: common

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類(17)

          隨筆檔案(28)

          文章分類(30)

          文章檔案(30)

          相冊(cè)

          收藏夾(2)

          hibernate

          java基礎(chǔ)

          mysql

          xml

          關(guān)注

          壓力測(cè)試

          算法

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 96750
          • 排名 - 600

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 黄平县| 宣恩县| 西丰县| 双牌县| 蒙城县| 会理县| 林西县| 乐至县| 中江县| 姜堰市| 忻城县| 夏津县| 阳城县| 盐边县| 广宗县| 农安县| 平谷区| 台东县| 罗城| 大同市| 永安市| 遂平县| 黄平县| 葵青区| 达拉特旗| 蒙山县| 腾冲县| 泗水县| 辽源市| 杂多县| 邹城市| 确山县| 茌平县| 洪洞县| 边坝县| 嘉兴市| 稷山县| 衡阳市| 五台县| 昌乐县| 泾阳县|