栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java编程图片(java画图图片大全)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java编程图片(java画图图片大全)

随笔——Java中常用的图片操作 导入的包
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
图片的缩放
public static BufferedImage scale(BufferedImage imageToScale, int dWidth, int dHeight) {
	
    BufferedImage scaledImage = null;
    if (imageToScale != null) {
        scaledImage = new BufferedImage(dWidth, dHeight, imageToScale.getType());
        Graphics2D graphics2D = scaledImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(imageToScale, 0, 0, dWidth, dHeight, null);
        graphics2D.dispose();
    }
    return scaledImage;
}
byte数组转BufferedImage
public static BufferedImage byteToBufferedImage(byte[] b) {
    
    try {
        ByteArrayInputStream in = new ByteArrayInputStream(b);
        return ImageIO.read(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
BufferedImage转byte数组
public static byte[] bufferedImageToByte(BufferedImage image) {
    
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, "jpg", out);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}
保存byte数组为图片到指定的路径
public static void saveByteToImage(byte[] b, String filepath) {
    
    try {
        FileOutputStream fileOutputStream = new FileOutputStream(filepath);
        fileOutputStream.write(b);
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
BufferedImage转InputStream
public static InputStream bufferedImageToInputStream(BufferedImage image) {
    
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", os);
        return new ByteArrayInputStream(os.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
保存BufferedImage到指定的路径
public static void saveBufferedImage(BufferedImage image, String filepath) {
    
    try {
        File file = new File(filepath);
        ImageIO.write(image, "jpg", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

创作不易,喜欢的话加个关注点个赞,❤谢谢谢谢❤

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号