tinguo002

           

          圖片轉(zhuǎn)字符串


          事例1:
          package com.apexsoft.mobile.utils;

          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;

          import sun.misc.BASE64Decoder;
          import sun.misc.BASE64Encoder;

          public class ImgToString {

              
          public static String getImageStr(String imgFile) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理
                  
          // String imgFile = "d:\\111.jpg";// 待處理的圖片
                  InputStream in = null;
                  
          byte[] data = null;
                  
          // 讀取圖片字節(jié)數(shù)組
                  try {
                      in 
          = new FileInputStream(imgFile);
                      data 
          = new byte[in.available()];
                      in.read(data);
                      in.close();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

                  
          // 對(duì)字節(jié)數(shù)組Base64編碼
                  BASE64Encoder encoder = new BASE64Encoder();
                  
          return encoder.encode(data);// 返回Base64編碼過(guò)的字節(jié)數(shù)組字符串
              }


              
          /**
               * 將字符串轉(zhuǎn)為圖片
               * 
               * 
          @param imgStr
               * 
          @return
               
          */

              
          public static boolean generateImage(String imgStr, String imgFile)
                      
          throws Exception {// 對(duì)字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片
                  if (imgStr == null// 圖像數(shù)據(jù)為空
                      return false;
                  BASE64Decoder decoder 
          = new BASE64Decoder();
                  
          try {
                      
          // Base64解碼
                      byte[] b = decoder.decodeBuffer(imgStr);
                      
          for (int i = 0; i < b.length; ++i) {
                          
          if (b[i] < 0{// 調(diào)整異常數(shù)據(jù)
                              b[i] += 256;
                          }

                      }

                      
          // 生成jpeg圖片
                      String imgFilePath = imgFile;// 新生成的圖片
                      OutputStream out = new FileOutputStream(imgFilePath);
                      out.write(b);
                      out.flush();
                      out.close();
                      
          return true;
                  }
           catch (Exception e) {
                      
          throw e;
                  }

              }


              
          public static void main(String[]args){
                  String imgStr 
          = ImgToString.getImageStr("C:\\Users\\think\\Pictures\\打開(kāi)Intenet選項(xiàng)1.jpg");
                  System.out.println(imgStr);
                  
          try {
                      ImgToString.generateImage(imgStr, 
          "D:\\3.jpg");
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }

              
          }








          事例2:
          package com.apexsoft.mobile.utils;

          import java.awt.image.BufferedImage;
          import java.io.BufferedInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.FileInputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.RandomAccessFile;
          import java.net.HttpURLConnection;
          import java.net.URL;
          import java.net.URLConnection;

          import javax.imageio.ImageIO;

          public class ImgToString2 {

              
          /**
               * @Title            getImgeHexString
               * @Description     網(wǎng)絡(luò)圖片轉(zhuǎn)換成二進(jìn)制字符串
               * 
          @param URLName    網(wǎng)絡(luò)圖片地址
               * 
          @param type        圖片類型
               * 
          @return    String    轉(zhuǎn)換結(jié)果
               * 
          @throws
               
          */

              
          /*
              public static String getImgeHexString(String URLName,String type) {
                  String res = null;
                  try {
                      int HttpResult = 0; // 服務(wù)器返回的狀態(tài)
                      URL url = new URL(URLName); // 創(chuàng)建URL
                      URLConnection urlconn = url.openConnection(); // 試圖連接并取得返回狀態(tài)碼
                      urlconn.connect();
                      HttpURLConnection httpconn = (HttpURLConnection) urlconn;
                      HttpResult = httpconn.getResponseCode();
                      System.out.println(HttpResult);
                      if (HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK則連接不成功
                          System.out.print("fail");
                      else {
                          BufferedInputStream bis = new BufferedInputStream(urlconn.getInputStream());

                          BufferedImage bm = ImageIO.read(bis);
                          ByteArrayOutputStream bos = new ByteArrayOutputStream();
                          ImageIO.write(bm, type, bos);
                          bos.flush();
                          byte[] data = bos.toByteArray();

                          res = byte2hex(data);
                          bos.close();
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return res;
              }
          */

              
          public static void main(String[]args){
                  String imgStr 
          = ImgToString2.getImgeHexString("C:\\Users\\think\\Pictures\\打開(kāi)Intenet選項(xiàng)1.jpg");
                  System.out.println(imgStr);
                  
              }

              
          public static String getImgeHexString(String imgFile) {
                  String res 
          = null;
                  
          try {
                          InputStream in 
          = null;
                          
          byte[] data = null;
                          
          // 讀取圖片字節(jié)數(shù)組
                          
                              in 
          = new FileInputStream(imgFile);
                              data 
          = new byte[in.available()];
                              in.read(data);
                              in.close();
                          
                          BufferedInputStream bis 
          = new BufferedInputStream(in);

                          BufferedImage bm 
          = ImageIO.read(bis);
                          ByteArrayOutputStream bos 
          = new ByteArrayOutputStream();
                          ImageIO.write(bm, 
          "jpg", bos);
                          bos.flush();
                          data 
          = bos.toByteArray();

                          res 
          = byte2hex(data);
                          bos.close();
                      
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

                  
          return res;
              }

              
              
          /**
               * @title            根據(jù)二進(jìn)制字符串生成圖片
               * 
          @param data         生成圖片的二進(jìn)制字符串
               * 
          @param fileName     圖片名稱(完整路徑)
               * 
          @param type        圖片類型
               * 
          @return
               
          */

              
          public static void saveImage(String data, String fileName,String type) {

                  BufferedImage image 
          = new BufferedImage(300300,BufferedImage.TYPE_BYTE_BINARY);
                  ByteArrayOutputStream byteOutputStream 
          = new ByteArrayOutputStream();
                  
          try {
                      ImageIO.write(image, type, byteOutputStream);
                      
          // byte[] date = byteOutputStream.toByteArray();
                      byte[] bytes = hex2byte(data);
                      System.out.println(
          "path:" + fileName);
                      RandomAccessFile file 
          = new RandomAccessFile(fileName, "rw");
                      file.write(bytes);
                      file.close();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

              }

              
              
          /**
               * 反格式化byte
               * 
               * 
          @param s
               * 
          @return
               
          */

              
          public static byte[] hex2byte(String s) {
                  
          byte[] src = s.toLowerCase().getBytes();
                  
          byte[] ret = new byte[src.length / 2];
                  
          for (int i = 0; i < src.length; i += 2{
                      
          byte hi = src[i];
                      
          byte low = src[i + 1];
                      hi 
          = (byte) ((hi >= 'a' && hi <= 'f'? 0x0a + (hi - 'a')
                              : hi 
          - '0');
                      low 
          = (byte) ((low >= 'a' && low <= 'f'? 0x0a + (low - 'a')
                              : low 
          - '0');
                      ret[i 
          / 2= (byte) (hi << 4 | low);
                  }

                  
          return ret;
              }


              
          /**
               * 格式化byte
               * 
               * 
          @param b
               * 
          @return
               
          */

              
          public static String byte2hex(byte[] b) {
                  
          char[] Digit = '0''1''2''3''4''5''6''7''8''9''A',
                          
          'B''C''D''E''F' }
          ;
                  
          char[] out = new char[b.length * 2];
                  
          for (int i = 0; i < b.length; i++{
                      
          byte c = b[i];
                      out[i 
          * 2= Digit[(c >>> 4& 0X0F];
                      out[i 
          * 2 + 1= Digit[c & 0X0F];
                  }


                  
          return new String(out);
              }

              
          }



           

          歡迎大家訪問(wèn)我的個(gè)人網(wǎng)站 萌萌的IT人

          posted on 2014-08-05 11:30 一堣而安 閱讀(1554) 評(píng)論(0)  編輯  收藏 所屬分類: java

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 怀集县| 招远市| 保定市| 通海县| 巩留县| 米易县| 图木舒克市| 临沭县| 会泽县| 绥滨县| 繁昌县| 浦城县| 呼伦贝尔市| 淄博市| 道孚县| 隆安县| 武城县| 中山市| 梧州市| 樟树市| 榆社县| 大关县| 全州县| 涞水县| 济源市| 依安县| 平凉市| 怀远县| 周口市| 北辰区| 麻城市| 聂荣县| 道真| 漳州市| 楚雄市| 华宁县| 榆林市| 互助| 周至县| 重庆市| 元谋县|