Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
/** * Convert binary flow to picture file * @author Night Breeze Studio Www.soservers.com */public class Imgertofileutil {/** * converts received string to picture save
* @param imgstr binary Stream Conversion String * @param imgpath Picture's save path * @param the name of the imgname picture * @return * 1: Save normal * 0: Save Failure * * public static int Savetoimgbystr (String imgstr,string imgpath,string imgname) {try {System.out.println ("===imgstr.leng
Th () ====> "+ imgstr.length () +" =====imgstr=====> "+ imgstr);
catch (Exception e) {e.printstacktrace ();}
int stateint = 1; if (imgstr!= null && imgstr.length () > 0) {try {//convert string to binary to display picture//format string of picture generated above IMGSTR
, revert to picture display byte[] Imgbyte = Hex2byte (IMGSTR);
InputStream in = new Bytearrayinputstream (imgbyte); File File=new file (imgpath,imgname);//can be any picture format. jpg,.png FileOutputStream fos=new FileoutputstreAM (file);
Byte[] B = new byte[1024];
int nread = 0;
while ((Nread = In.read (b))!=-1) {fos.write (b, 0, nread);
} fos.flush ();
Fos.close ();
In.close ();
catch (Exception e) {stateint = 0;
E.printstacktrace ();
finally {}} return stateint; /** * Convert binary to Picture save * @param imgstr binary Stream Conversion String * @param imgpath Picture's save path * @param imgname the name of the picture * @return * 1: Save Normal * 0: Save failed/public static int savetoimgbybytes (File imgfile,string imgpath,string imgname) {int Statei
NT = 1; if (imgfile.length () > 0) {try {file File=new file (imgpath,imgname);//can be any picture format. jpg,.png FileOutputStream F
Os=new fileoutputstream (file);
FileInputStream fis = new FileInputStream (imgfile);
Byte[] B = new byte[1024];
int nread = 0;
while ((Nread = Fis.read (b))!=-1) {fos.write (b, 0, nread);
} fos.flush ();
Fos.close ();
Fis.close (); catch (Exception e) {stateInt = 0;
E.printstacktrace ();
finally {}} return stateint; /** * Binary Spin String * @param b * @return/public static string Byte2hex (byte[] b)//binary turn string {stringbuffer SB
= new StringBuffer ();
String stmp = "";
for (int n = 0; n < b.length n++) {stmp = Integer.tohexstring (B[n] & 0XFF);
if (stmp.length () = = 1) {sb.append ("0" + stmp);
else {sb.append (STMP);
} return sb.tostring (); /** * String binary * @param str the string to be converted * @return converted binary array/public static byte[] Hex2byte (String str) {//String
binary if (str = null) return null;
str = Str.trim ();
int len = Str.length ();
if (len = = 0 | | | len% 2 = 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; }
}
}