1 ImportJava.awt.image.BufferedImage; 2 ImportJava.io.ByteArrayInputStream; 3 ImportJava.io.ByteArrayOutputStream; 4 ImportJava.io.File; 5 Importjava.io.IOException; 6 7 ImportJavax.imageio.ImageIO; 8 9 ImportSun.misc.BASE64Decoder; Ten ImportSun.misc.BASE64Encoder; One A Public classTestimagebinary { - StaticBase64encoder encoder =NewSun.misc.BASE64Encoder (); - StaticBase64decoder decoder =NewSun.misc.BASE64Decoder (); the - Public Static voidMain (string[] args) { - System.out.println (Getimagebinary ()); - + Base64stringtoimage (Getimagebinary ()); - } + A StaticString getimagebinary () { atFile f =NewFile ("C://20090709442.jpg"); - bufferedimage Bi; - Try { -BI =Imageio.read (f); -Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); -Imageio.write (BI, "JPG", BAOs); in byte[] bytes =Baos.tobytearray (); - to returnencoder.encodebuffer (bytes). Trim (); +}Catch(IOException e) { - E.printstacktrace (); the } * return NULL; $ } Panax Notoginseng - Static voidbase64stringtoimage (String base64string) { the Try { + byte[] Bytes1 =Decoder.decodebuffer (base64string); A theBytearrayinputstream Bais =NewBytearrayinputstream (bytes1); +BufferedImage BI1 =Imageio.read (Bais); -File W2 =NewFile ("C://qq.bmp");//can be jpg,png,gif format $Imageio.write (bi1, "JPG", W2);//No matter what the format of the output picture, here do not need to change $}Catch(IOException e) { - E.printstacktrace (); - } the } - Wuyi}
The problem is: the color of the converted picture is incorrect
Other person code:
1 Packagecn.com;2 3 ImportJava.io.FileInputStream;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.InputStream;7 ImportJava.io.OutputStream;8 9 ImportSun.misc.BASE64Decoder;Ten ImportSun.misc.BASE64Encoder; One A Public classbase64test - { - Public Static voidMain (string[] args) the { -String strimg =getimagestr (); - System.out.println (strimg); - generateimage (strimg); + } - //Convert images into base64 strings + Public StaticString getimagestr () A{//Converts a picture file into a byte array string and Base64 it for encoding atString imgfile = "D://test.jpg";//pictures to be processed -InputStream in =NULL; - byte[] data =NULL; - //reading a picture byte array - Try - { inin =NewFileInputStream (Imgfile); -data =New byte[In.available ()]; to in.read (data); + in.close (); - } the Catch(IOException e) * { $ e.printstacktrace ();Panax Notoginseng } - //Base64 encoding of byte arrays theBase64encoder encoder =NewBase64encoder (); + returnEncoder.encode (data);//returns a BASE64 encoded byte array string A } the + //base64 strings into pictures - Public Static Booleangenerateimage (String imgstr) ${//Base64 decoding a byte array string and generating a picture $ if(Imgstr = =NULL)//image data is empty - return false; -Base64decoder decoder =NewBase64decoder (); the Try - {Wuyi //Base64 decoding the byte[] B =Decoder.decodebuffer (IMGSTR); - for(inti=0;i<b.length;++i) Wu { - if(b[i]<0) About{//Adjust Exception Data $b[i]+=256; - } - } - //Create a JPEG picture AString Imgfilepath = "D://222.jpg";//the newly generated picture +OutputStream out =NewFileOutputStream (Imgfilepath); the Out.write (b); - Out.flush (); $ out.close (); the return true; the } the Catch(Exception e) the { - return false; in } the } the}
Java Picture binary conversion