JAVA_将二进制流转换成图片文件

1.  [代码]将二进制流转换成图片文件 晚风工作室 www.soservers.com     跳至 [1] [全屏预览]
view source print ?
001 importjava.io.ByteArrayInputStream;
002 importjava.io.File;
003 importjava.io.FileInputStream;
004 importjava.io.FileOutputStream;
005 importjava.io.InputStream;
006  
007 /**
008  * 将二进制流转换成图片文件
009  * @author 晚风工作室 www.soservers.com
010  *
011  */
012  
013 publicclassImgErToFileUtil {
014      
015     /**
016      * 将接收的字符串转换成图片保存
017      * @param imgStr 二进制流转换的字符串
018      * @param imgPath 图片的保存路径
019      * @param imgName 图片的名称
020      * @return
021      *      1:保存正常
022      *      0:保存失败
023      */
024     publicstaticintsaveToImgByStr(String imgStr,String imgPath,String imgName){
025 try{
026     System.out.println("===imgStr.length()====>"+ imgStr.length()
027             +"=====imgStr=====>"+ imgStr);
028 }catch(Exception e) {
029     e.printStackTrace();
030 }
031         intstateInt =1;
032         if(imgStr !=null&& imgStr.length() >0){
033             try{
034                  
035                 // 将字符串转换成二进制,用于显示图片 
036                 // 将上面生成的图片格式字符串 imgStr,还原成图片显示 
037                 byte[] imgByte = hex2byte( imgStr ); 
038      
039                 InputStream in =newByteArrayInputStream(imgByte);
040      
041                 File file=newFile(imgPath,imgName);//可以是任何图片格式.jpg,.png等
042                 FileOutputStream fos=newFileOutputStream(file);
043                    
044                 byte[] b =newbyte[1024];
045                 intnRead =0;
046                 while((nRead = in.read(b)) != -1) {
047                     fos.write(b,0, nRead);
048                 }
049                 fos.flush();
050                 fos.close();
051                 in.close();
052      
053             }catch(Exception e) {
054                 stateInt =0;
055                 e.printStackTrace();
056             }finally{
057             }
058         }
059         returnstateInt;
060     }
061      
062     /**
063      * 将二进制转换成图片保存
064      * @param imgStr 二进制流转换的字符串
065      * @param imgPath 图片的保存路径
066      * @param imgName 图片的名称
067      * @return
068      *      1:保存正常
069      *      0:保存失败
070      */
071     publicstaticintsaveToImgByBytes(File imgFile,String imgPath,String imgName){
072  
073         intstateInt =1;
074         if(imgFile.length() >0){
075             try{
076                 File file=newFile(imgPath,imgName);//可以是任何图片格式.jpg,.png等
077                 FileOutputStream fos=newFileOutputStream(file);
078                  
079                 FileInputStream fis =newFileInputStream(imgFile);
080                    
081                 byte[] b =newbyte[1024];
082                 intnRead =0;
083                 while((nRead = fis.read(b)) != -1) {
084                     fos.write(b,0, nRead);
085                 }
086                 fos.flush();
087                 fos.close();
088                 fis.close();
089      
090             }catch(Exception e) {
091                 stateInt =0;
092                 e.printStackTrace();
093             }finally{
094             }
095         }
096         returnstateInt;
097     }
098  
099     /**
100      * 二进制转字符串
101      * @param b
102      * @return
103      */
104     publicstaticString byte2hex(byte[] b)// 二进制转字符串
105     {
106         StringBuffer sb =newStringBuffer();
107         String stmp ="";
108         for(intn =0; n < b.length; n++) {
109             stmp = Integer.toHexString(b[n] &0XFF);
110             if(stmp.length() ==1) {
111                 sb.append("0"+ stmp);
112             }else{
113                 sb.append(stmp);
114             }
115  
116         }
117         returnsb.toString();
118     }
119  
120     /**
121      * 字符串转二进制
122      * @param str 要转换的字符串
123      * @return  转换后的二进制数组
124      */
125     publicstaticbyte[] hex2byte(String str) {// 字符串转二进制
126         if(str ==null)
127             returnnull;
128         str = str.trim();
129         intlen = str.length();
130         if(len ==0|| len %2==1)
131             returnnull;
132         byte[] b =newbyte[len /2];
133         try{
134             for(inti =0; i < str.length(); i +=2) {
135                 b[i /2] = (byte) Integer
136                         .decode("0X"+ str.substring(i, i +2)).intValue();
137             }
138             returnb;
139         }catch(Exception e) {
140             returnnull;
141         }
142     }
143      
144 }

你可能感兴趣的:(java,二进制)