码迷,mamicode.com
首页 > 其他好文 > 详细

把图片生成Base64字符串

时间:2015-06-30 20:18:02      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }

 

把图片生成Base64字符串

标签:

原文地址:http://www.cnblogs.com/yshyee/p/4611264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!