JAVA converts a picture to Base64 encoding

Source: Internet
Author: User
Tags base64



The jar package used here is Commons-codec-1.10.jar;



Sample code


1 import java.io.FileInputStream;
 2 import java.io.FileOutputStream;
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6
 7 import org.apache.commons.codec.binary.Base64;
 8 
 9 
10 /**
11 * Convert images to Base64<br>
12 * Decode base64 encoded string into img image
13 * @Create time 2015-06-01 15:50
14 *
15 */
16 public class Img2Base64Util {
17
18 public static void main(String[] args) {
19 String imgFile = "d:\\3.jpg";//Pending images
20 String imgbese=getImgStr(imgFile);
21 System.out.println(imgbese.length());
22 System.out.println(imgbese);
23 String imgFilePath = "d:\\332.jpg";//Newly generated image
24 generateImage(imgbese,imgFilePath);
25 }
26 /**
27 * Convert images to Base64 encoding
28 * @param imgFile Pending image
29 * @return
30 */
31 public static String getImgStr(String imgFile){
32 //Convert the image file into a byte array string and perform Base64 encoding on it.
33
34
35 InputStream in = null;
36 byte[] data = null;
37 //Read image byte array
38 try
39 {
40 in = new FileInputStream(imgFile);
41 data = new byte[in.available()];
42 in.read(data);
43 in.close();
44 }
45 catch (IOException e)
46 {
47 e.printStackTrace();
48 }
49 return new String(Base64.encodeBase64(data));
50 }
51
52 /**
53 * Base64 decoding of byte array strings and generating images
54 * @param imgStr image data
55 * @param imgFilePath Save the image full path address
56 * @return
57 */
58 public static boolean generateImage(String imgStr,String imgFilePath){
59 //
60 if (imgStr == null) //The image data is empty
61 return false;
62
63 try
64 {
65 //Base64 decoding
66 byte[] b = Base64.decodeBase64(imgStr);
67 for(int i=0;i<b.length;++i)
68 {
69 if(b[i]<0)
70 {//Adjust exception data
71 b[i]+=256;
72 }
73 }
74 //Generate jpeg image
75
76 OutputStream out = new FileOutputStream(imgFilePath);
77 out.write(b);
78 out.flush();
79 out.close();
80 return true;
81 }
82 catch (Exception e)
83 {
84 return false;
85 }
86 }
87 } 





Attach an example of displaying a picture in HTML;



The converted picture Base64 encoded string is/9j/4aaqskzjrgabaqeayabgaad/2wbdaaibaqibaqicagicagicawud ...



First add the converted string to the header file ;


<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAArAGQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD7y+b3/MUfN7/mK6b/AIQ1v7hr5l/4KwftfeLP+Cc/7Mv/AAn3h74bS+N4J52sJr+e8MOn+Hp32rbS3UaL5ssMjllwjR/MqqXUyLX4NhqFevVjRprV6LZH8/YNYnE1o0KXxS26HunzcZzz7ij5/f8AMV+Jfxs/4ONPiH8Y/gB4P0j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BUEYIr0X+z4f7h/76NH9nQ/3D/wB9GvKjhZp3TSPAjgakXeLS+8yXgDuWYlmJ6k5NDQiRgzFmI7k5Nap02E9UP/fRpf7Oh/uH/vo0nhJeQnl8vIyPsq9cCj7KvoK1/wCz4f7h/wC+jR/Z8P8AcP8A30aPqcvIP7Pl5f18jI+yr6Cj7KvoK1/7Ph/uH/vo0f2fD/cP/fRo+py8g/s+Xl/XyOd17wtpvinTHsdU0+x1OxlZGkt7uBZ4XKOHQlHBBKuqsDjhlBHIFWjbLwSBk/rWx/Z0P9w/99Gk/s2A/wAB/wC+jR9Ula2gf2fPbQyltlXkAAn04pPsyqBwOK1/7Ph/uH/vo0f2dD/cP/fRo+py8gWXS8v6+RqeC0CaQwAwPMP8hRVjw9CtvYsqDC7yeufSivew8eWnGPkfU4SHJRjHsj//2Q==
">



JAVA converts a picture to Base64 encoding


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.