public class ImgeUtils { public static String img2String(BufferedImage img,String type){ String imgStr = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(img, type, baos); byte[] imgByte = baos.toByteArray(); imgStr = new BASE64Encoder().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); } } return imgStr; } }
public static void main(String[] args) { String imgStr = null; try { BufferedImage img = Thumbnails.of( ImageIO.read(new File("/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); }
时间: 2024-11-10 22:55:21