Conversion between Java IO stream picture and string

Source: Internet
Author: User

Package Com.yundongsports.arena.controller.basketballsite;import Sun.misc.base64decoder;import Sun.misc.base64encoder;import java.io.*;/** * Created by CF on 2016/12/23.    */public class Test {String c= ""; /** * Convert string to Picture * @param imgstr * @return * */public static boolean generateimage (string imgstr,string im        Gfile) throws Exception {///byte array string Base64 decoded and generate picture if (imgstr = = Null)//image data is empty return false;        Base64decoder decoder = new Base64decoder ();            Try {//Base64 decode byte[] b = Decoder.decodebuffer (imgstr);                for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {//adjust exception data b[i] + = 256; }}//generate jpeg picture String Imgfilepath = "d:\\imgg\\c.jpg";//newly generated picture OutputS            Tream out = new FileOutputStream (imgfilepath);            Out.write (b);            Out.flush ();            Out.close ();        Return true; } catch (EXception E) {throw e; }} public static string Getimagestr (string Imgfile) {//converts The picture file to a byte array string and Base64 encodes it//string imgfile = "d:\\111.        JPG ";//picture to be processed inputstream in = null;            byte[] data = null;//reads a picture byte array try {in = new FileInputStream (imgfile);            data = new Byte[in.available ()];            In.read (data);        In.close ();        } catch (ioexception E) {e.printstacktrace ();        }//BASE64 encoding of byte array Base64encoder encoder = new Base64encoder ();        Return Encoder.encode (data);//returns Base64 encoded byte array string} public static void main (string[] Args) throws Exception {        String Aa=getimagestr ("d:\\img\\a.png");        System.out.print (aa);    Generateimage (aa, ""); }}

  

Conversion between Java IO stream picture and string

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.