zoukankan      html  css  js  c++  java
  • base64转byte[]、byte[]转base64、byte[]转图片后图片按固定宽高缩放

    /**
         * 图片缩放
         */
        public static BufferedImage ImageStringByte(int width, byte[] b) {
            
            InputStream buffin = new ByteArrayInputStream(b);
            BufferedImage src = null;
            try {
                src = ImageIO.read(buffin);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // 读入文件
            int widthYuan = src.getWidth(); // 得到源图宽
            int heightYuan = src.getHeight(); // 得到源图长
            heightYuan = heightYuan / (widthYuan/width);
            widthYuan = width ;
            Image image = src.getScaledInstance(widthYuan, heightYuan, Image.SCALE_DEFAULT);
            BufferedImage tag = new BufferedImage(widthYuan, heightYuan, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            boolean drawImage = g.drawImage(image, 0, 0, null); // 绘制缩小后的图
            g.dispose();
           return tag;// 输出到文件流
        }

    /**
         * byte流转为字符串
         */
        public static String stringsByImage(byte[] binaryData) {
            String b = null;
            try {
                // b = decoder.decodeBuffer(base64str);
                b = Base64.encode(binaryData);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return b;
        }

        /**
         * 将图片字符串流转为图片二进制
         */
        public static byte[] ImageByString(String base64str) {
            byte[] b = null;
            try {
                b = Base64.decode(base64str);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return b;
        }

    /**
         * 将图片直接转为byte
         */
        public static byte[] byteByImage(BufferedImage  image) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 将图片转为byte
            try {
                ImageIO.write(image, "jpg", bos);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return bos.toByteArray();

        }

  • 相关阅读:
    React之改变页面上方图标
    箭头函数参数解构
    h5项目(特别是vue)缓存严重的解决方案,配合nginx
    springboot中配置urlrewrite实现url伪静态强化网站seo
    使用docker创建mongodb
    mac下sourcetree创建git分支和合并分支
    在系统下文件上传报错:The temporary upload location [/tmp/tomcat.xxx/work/Tomcat/localhost/ROOT] is not valid
    解决Mac下SourceTree每次都让输入密码的问题
    快速生成mysql上百万条测试数据
    10年前错过比特币,如今有斯坦福区块链项目pi币,对标btc,手机免费挖矿详细教程。
  • 原文地址:https://www.cnblogs.com/han-java/p/10502448.html
Copyright © 2011-2022 走看看