Public classImgeutils { Public Staticstring img2string (bufferedimage img,string type) {string Imgstr=NULL; Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Try{imageio.write (img, type, BAOS); byte[] Imgbyte =Baos.tobytearray (); Imgstr=NewBase64encoder (). Encode (Imgbyte); } Catch(IOException ex) {Logger.getlogger (imgeutils.class. GetName ()). log (Level.severe,NULL, ex); }finally{ Try{baos.close (); } Catch(IOException ex) {Logger.getlogger (imgeutils.class. GetName ()). log (Level.severe,NULL, ex); } } returnImgstr; }}
Public Static voidMain (string[] args) {String imgstr=NULL; Try{bufferedimage img=Thumbnails.of (Imageio.read (NewFile ("/home/y/my_screen/markers.png")) . Size (100, 100). Asbufferedimage (); Imgstr= Imgeutils.img2string (img, "PNG"); } Catch(IOException ex) {Logger.getlogger (pngbase64test.class. GetName ()). log (Level.severe,NULL, ex); } System.out.println ("Imgstr:" +imgstr); }
Generate Base64 strings for pictures