Java converts pictures to BASE64 encoding and inversion methods

Source: Internet
Author: User
Tags base64
In the project, you need to convert the BASE64 encoded picture into a picture file displayed in a Web project, Google search, you write a interchangeable tool class
Package com.base.util;
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;  /** * Picture and BASE64-encoded intermodulation tool class * @author Wangwei * * */public class Imagebase64util {public static void main (string[) args) {//test conversion from BASE64 encoding to picture file//String strimg = "xxxxx"; \ picture BASE64 string//Generateimage (strimg, "D
        : \\wangyc.jpg ");
        
        The test converts from picture file to Base64 encoded//SYSTEM.OUT.PRINTLN (GETIMAGESTR ("d:\\0.jpg"));
    Generateimage (Getimagestr ("d:\\0.jpg"), "d:\\000.jpg"); /** * Converts a picture file into a byte array string and Base64 it for encoding * @param imgfilepath picture path * @return String/Pub
        Lic static string Getimagestr (String imgfilepath) {byte[] data = null;
            Reads the picture byte array try {InputStream in = new FileInputStream (Imgfilepath); data = new Byte[in.available ()];
            In.read (data);
        In.close ();
        catch (IOException e) {e.printstacktrace ();
        ///To byte array Base64 encoding Base64encoder encoder = new Base64encoder (); return Encoder.encode (data)//Returns BASE64 encoded byte array string}/** * Base64 decoding of byte array strings and generating pictures * @param imgstr Base6 4 String * @param imgfilepath Generate picture Save path * @return Boolean/public static Boolean Generateimage (String img
        STR, String Imgfilepath) {if (imgstr = = null)//image data is null return false;
        Base64decoder decoder = new Base64decoder ();
            try {//Base64 decode byte[] bytes = Decoder.decodebuffer (IMGSTR); for (int i = 0; i < bytes.length ++i) {if (bytes[i) < 0) {//Adjust exception data Bytes[i] +
                = 256;
            }///generate JPEG picture outputstream out = new FileOutputStream (Imgfilepath);
       Out.write (bytes);     Out.flush ();
            Out.close ();
        return true;
        catch (Exception e) {return false; /** * Base64 decoding the byte array string and generating a picture * @param imgstr picture String * @return byte[] * * Public St
        Atic byte[] Getstrtobytes (String imgstr) {if (IMGSTR = null)//image data is null return null;
        Base64decoder decoder = new Base64decoder ();
            try {//Base64 decode byte[] bytes = Decoder.decodebuffer (IMGSTR); for (int i = 0; i < bytes.length ++i) {if (bytes[i) < 0) {//Adjust exception data Bytes[i] +
                = 256;
        }///Generate JPEG picture return bytes;
        catch (Exception e) {return null;  }
    }
}
 

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.