- 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 Test64Bit {
- public static void main(String[] args) {
- // 测试从Base64编码转换为图片文件
- String strImg = "这里放64位编码";
- GenerateImage(strImg, "D:\wangyc.jpg");
- // 测试从图片文件转换为Base64编码
- System.out.println(GetImageStr("d:\wangyc.jpg"));
- }
- public static String GetImageStr(String imgFilePath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
- byte[] data = null;
- // 读取图片字节数组
- try {
- InputStream in = new FileInputStream(imgFilePath);
- 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编码过的字节数组字符串
- }
- public static boolean GenerateImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
- if (imgStr == null) // 图像数据为空
- return false;
- BASE64Decoder decoder = new BASE64Decoder();
- try {
- // Base64解码
- byte[] bytes = decoder.decodeBuffer(imgStr);
- for (int i = 0; i < bytes.length; ++i) {
- if (bytes[i] < 0) {// 调整异常数据
- bytes[i] += 256;
- }
- }
- // 生成jpeg图片
- OutputStream out = new FileOutputStream(imgFilePath);
- out.write(bytes);
- out.flush();
- out.close();
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- }
java利用Base64编码和解码图片文件
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* @author zxn
* @version 创建时间:2014-7-2 上午11:40:40
*
*/
public class ImageUtils {
/**
* 将网络图片进行Base64位编码
*
* @param imgUrl
* 图片的url路径,如http://.....xx.jpg
* @return
*/
public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageUrl);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
}
/**
* 将本地图片进行Base64位编码
*
* @param imgUrl
* 图片的url路径,如http://.....xx.jpg
* @return
*/
public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageFile);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
}
/**
* 将Base64位编码的图片进行解码,并保存到指定目录
*
* @param base64
* base64编码的图片信息
* @return
*/
public static void decodeBase64ToImage(String base64, String path,
String imgName) {
BASE64Decoder decoder = new BASE64Decoder();
try {
FileOutputStream write = new FileOutputStream(new File(path
+ imgName));
byte[] decoderBytes = decoder.decodeBuffer(base64);
write.write(decoderBytes);
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、如何直接在页面上显示base64编码的图片?
样例如下:
<html>
<body>
<img src='data:image/jpg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMj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nxl/5E22/wCv5P8A0B65Hw38Vv8AhH/D9ppX9i/aPs6keb9q2bssT02HHX1r3GivnJ0ZOfPGVvke9h8yoQwqw1ejzpO/xNfkjyP/AIXf/wBS9/5O/wD2uuL8S+JJPGfiezvI7BoHCJAsKv5hbDE56D+90xX0hRUTw9SatKenob4fNsJhp+0o4a0v8bf5o8i+N/39D+k//tOm+IviTplwt3ol/wCGxeJBI0IL3GMlSV3DC5U8djmvX6KqVCTlKUZWv5GNHNKMaNOlVpc3Jez5mt3foj5wsPDvi6XQ724sLa/h048vArsvmj2T+PHfitfwv480rwpa+UnhgC+A2zXHn/O579VJX6DiveKKiOEcGnGWvpc6qufxrxlCvRvFvpJx++25BZ3H2uxt7nbs86JZNuc4yM4zU9FFdh867N6BRRRQIKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/2Q=='/>
</body>
</html>
javascript从image转换为base64位编码的String
最近需要把app的微信分享方法开放给webview,涉及到分享的图片,如果通过传送图片连接,那将要在后台再取一次图片文件,会影响速度,我选择webview把image以base64位编码的方式传给本地应用。 下面是实现的参考代码:
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Image to Base64 - jsFiddle demo by handtrix</title> <script type='text/javascript' src='//code.jquery.com/jquery-2.0.2.js'></script> <link rel="stylesheet" type="text/css" href="/css/result-light.css" rel="external nofollow" > <style type='text/css'> @import url('//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css'); body{ padding: 20px; } </style> <script type='text/javascript'>//<![CDATA[ $(window).load(function(){ /** * convertImgToBase64 * @param {String} url * @param {Function} callback * @param {String} [outputFormat='image/png'] * @author HaNdTriX * @example convertImgToBase64('http://goo.gl/AOxHAL', function(base64Img){ console.log('IMAGE:',base64Img); }) */ function convertImgToBase64(url, callback, outputFormat){ var canvas = document.createElement('CANVAS'); var ctx = canvas.getContext('2d'); var img = new Image; img.crossOrigin = 'Anonymous'; img.onload = function(){ canvas.height = img.height; canvas.width = img.width; ctx.drawImage(img,0,0); var dataURL = canvas.toDataURL(outputFormat || 'image/png'); callback.call(this, dataURL); // Clean up canvas = null; }; img.src = url; } $('#img2b64').submit(function(event){ var imageUrl = $(this).find('input[name=url]').val(); console.log('imageUrl', imageUrl); convertImgToBase64(imageUrl, function(base64Img){ $('.output') .find('textarea') .val(base64Img) .end() .find('a') .attr('href', base64Img) .text(base64Img) .end() .find('img') .attr('src', base64Img); }); event.preventDefault(); }); });//]]> </script> </head> <body> <h2>Input</h2> <form class="input-group" id="img2b64"> <input type="url" name="url" class="form-control" placeholder="Insert an IMAGE-URL" value="http://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png" required> <span class="input-group-btn"> <input type="submit" class="btn btn-default"> </span> </form> <hr> <h2>Output</h2> <div class="output"> <textarea class="form-control"></textarea><br> <a></a><br><br> <img><br> </div> </body> </html>