java--convert binary to picture file __java

Source: Internet
Author: User
Tags stringbuffer
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; }
	}
	
}

Related Article

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.