Java画像からbase64文字列、base64文字列を描くには、特定のコンテンツは次のとおりです
1。画像からbase64文字列:
/ *** base64-Encoded String to Image* @Param IMGSTR Base64-Encoded String* @Param Path Image Path*/ public Static Boolean Base64StrtoImage(String Imgstr、String Path){if(imgstr == null)return false; base64Decoder decoder = new Base64Decoder(); try {// decrypt byte [] b = decoder.decodebuffer(imgstr); //(int i = 0; i <b.length; ++ i){if(b [i] <0){b [i] += 256; }} //フォルダーが存在しない場合、ファイルは自動的に作成されます。 tempfile = new file(path); if(!tempfile.getParentFile()。exists()){tempfile.getParentFile()。mkdirs(); } outputStream out = new fileoutputStream(tempfile); out.write(b); out.flush(); out.close(); trueを返します。 } catch(例外e){falseを返します。 }}2。Base64文字列への文字列:
/ *** base64文字列への画像* @param imgfile画像パス* @return*/ public static string imagetobase64str(string imgfile){inputstream inputstream = null; byte [] data = null; try {inputstream = new FileInputStream(imgfile); data = new byte [inputstream.abailable()]; inputstream.read(data); inputstream.close(); } catch(ioexception e){e.printstacktrace(); } // encryption base64encoder encoder = new base64Encoder(); return encoder.encode(data); }3。テスト:
public static void main(string [] args){string base64str = imagetobase64str( "d:/pic/001.jpg"); System.out.println(base64str); boolean b = base64strtoimage(base64str、 "d:/pic/temp/002.jpg"); System.out.println(b); }複製画像:
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。