圖片的base64編碼實現以及網頁上顯示

來源:互聯網
上載者:User

標籤:圖片   base64   html   

產生、解析base64編碼的圖片
//圖片轉化成base64字串      public static String GetImageStr(<span style="font-family: Arial, Helvetica, sans-serif;">String imgFile</span><span style="font-family: Arial, Helvetica, sans-serif;">)  </span>    {//將圖片檔案轉化為位元組數組字串,並對其進行Base64編碼處理            InputStream in = null;          byte[] data = null;          //讀取圖片位元組數組          try           {              in = new FileInputStream(imgFile);                      data = new byte[in.available()];              in.read(data);              in.close();          }           catch (IOException e)           {              e.printStackTrace();          }          //對位元組數組Base64編碼          BASE64Encoder encoder = new BASE64Encoder();          return encoder.encode(data);//返回Base64編碼過的位元組數組字串      }  <span style="white-space:pre"></span>//base64字串轉化成圖片      public static boolean GenerateImage(String imgStr,<span style="font-family: Arial, Helvetica, sans-serif;">String imgFilePath</span><span style="font-family: Arial, Helvetica, sans-serif;">)  </span>    {   //對位元組數組字串進行Base64解碼並產生圖片          if (imgStr == null) //映像資料為空白              return false;          BASE64Encoder decoder = new BASE64Encoder();          try           {              //Base64解碼              byte[] b = decoder.decode(imgStr);              for(int i=0;i<b.length;++i)              {                  if(b[i]<0)                  {//調整異常資料                      b[i]+=256;                  }              }              //新產生的圖片              OutputStream out = new FileOutputStream(imgFilePath);                  out.write(b);              out.flush();              out.close();              return true;          }           catch (Exception e)           {              return false;          }      }

網頁上顯示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>img</title>
<link rel="stylesheet" type="text/css" href="">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<img src=‘data:image/gif;base64,字串‘/>
</body>
</html>

圖片的base64編碼實現以及網頁上顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.