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

    }

转载于:https://www.cnblogs.com/han-java/p/10502448.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Java中的Base64图片,并设置图片宽高,可以按照以下步骤进行操作: 1. 首先,将Base64字符串解码为字节数组。可以使用Base64类提供的`getDecoder().decode()`方法来实现,其中`getDecoder()`返回一个Base64.Decoder对象,`decode()`方法将Base64字符串解码为字节数组。 2. 创建一个BufferedImage对象,通过调用`ImageIO.read()`方法,并传入解码后的字节数组作为参数,将字节数组换为BufferedImage对象。 3. 设置所需的图片宽高。可以通过调用BufferedImage对象的`getScaledInstance()`方法来实现,该方法返回一个按照指定宽高缩放后的新BufferedImage对象。可以将原始BufferedImage对象的宽高与目标宽高进行比例计算,得到所需缩放比例。 4. 将新的BufferedImage对象保存为图片文件或进行其他操作。 下面是一个示例代码,演示了如何将Base64字符串换为图片并设置宽高: ```java import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.util.Base64; import javax.imageio.ImageIO; public class ImageUtils { public static void main(String[] args) throws IOException { String base64Image = "your_base64_image_string"; // 替换成你的Base64字符串 // 解码Base64字符串为字节数组 byte[] imageBytes = Base64.getDecoder().decode(base64Image); // 将字节数组换为BufferedImage对象 BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageBytes)); // 设置图片宽高 int width = 500; // 设置宽度为500 int height = 300; // 设置高度为300 Image scaledImage = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); // 创建新的BufferedImage对象 BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); newImage.getGraphics().drawImage(scaledImage, 0, 0, null); // 保存新图片到文件 File outputFile = new File("output.png"); // 替换成你想保存的文件路径 ImageIO.write(newImage, "png", outputFile); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值