Spent the morning on the time summed up the various ways to convert the film, in this share.
import java.io.bytearrayoutputstream;import java.io.file;import java.util.base64;import javax.imageio.stream.fileimageinputstream;import javax.imageio.stream.fileimageoutputstream;public Class ccc {public static void main (String[] args) throws Exception{ File file=new file ("D://2.png");//file file2=new file ("d://3.png"); byte data[]= Imagetobyte (file);//string str=bytetohex (data);//byte data2[]=hextobyte (str); Bytetoimage ( Base64stringtobyte (bytetobase64string (data)), "D://aaaa.png");} Picture Turn binary public static byte[] imagetobyte (file file) throws exception{ Fileimageinputstream input =new fileimageinputstream (file); bytearrayoutputstream Output = new bytearrayoutputstream (); byte[] data = null; Byte[] buf = new byte[1024]; int numbytesread = 0; while ((Numbytesread = input.read (BUF)) != -1) { output.write (Buf, 0, numbytesread); } data = output.tobytearray () ; output.close (); input.close (); return data;} Binary Turn string Public static string bytetohex (byte[] b) throws exception{ stringbuffer sb = new stringbuffer (); String stmp = ""; for (int n = 0; n < b.length; n + +) { stmp = integer.tohExstring (B[N]&NBSP;&&NBSP;0XFF); if (Stmp.length () == 1) { sb.append ("0" + stmp); } else { sb.append (STMP); } } return Sb.tostring (); } //string into binary public static byte[] Hextobyte (STRING&NBSP;STR) { if (str == null) return null; str = str.trim (); int len = str.length (); if (len == 0 | | &NBSP;LEN&NBSP;%&NBSP;2&NBSP;==&NBSP;1) return null; byte[] b = new byte[len / 2]; try { for (int I = 0; i < str.length (); i += 2) { b[i / 2] = (Byte) integer.decode ("0X" + str.substring (i, i + 2)). IntValue (); } return b; } catch (exception e) { return null; } } //binary turn picture path for picture address public static Void bytetoimage (Byte[] data,string path) { if (data.length<3| | Path.equals ("")) return; try{ FileImageOutputStream Imageoutput = new fileimageoutputstream (New file (path)); Imageoutput.write (data, 0, d ata.length); imageoutput.close (); system.out.println ("Make Picture success,Please find image in " + path); } catch (Exception ex) { system.out.println ("Exception: " + ex"; ex.printstacktrace (); } }//binary byte[] to base64 string public static string bytetobase64string (byte[] data) {String base64string=base64.getencoder (). encodetostring (data); return base64string;} Base64 string Go binary byte[]public static byte[] base64stringtobyte (string base64string) {byte[] bytes = base64.getdecoder (). Decode (base64string); return bytes;}
The picture-to-string recommendation uses the Base64 conversion method, the Base64 string to the foreground can be displayed directly.
The format is
Note: The Util.base64 package here is jdk1.8 only.
Various conversions of pictures