直接看代码,做图片的颜色渲染.注意一个问题,流不能重复使用,还有一个很重要重要重要的一点
图片不要是百度搜索下载的图片 .webp 然后修改下后缀,当作图片去解析,会报错 报空指针的
public BufferedImage DyeImage(InputStream inputStream, String color){
try{
BufferedImage image = ImageIO.read(inputStream);
BufferedImage dye=null;
dye = dye(image, new Color(29, 245, 5, 42));
return dye;
}catch(IOException e){
e.printStackTrace();
throw new RestException("工作内容图片着色解析失败");
}
}
private BufferedImage dye(BufferedImage image, Color color){
int w = image.getWidth();
int h = image.getHeight();
BufferedImage dyed = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = dyed.createGraphics();
g.drawImage(image, 0,0, null);
g.setComposite(AlphaComposite.SrcAtop);
g.setColor(color);
g.fillRect(0,0,w,h);
g.dispose();
return dyed;
}
/**
* 转换BufferedImage 数据为byte数组
*
* Image对象
* @param format
* image格式字符串.如"gif","png"
* @return byte数组
*/
public static byte[] imageToBytes(BufferedImage bImage, String format) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, format, out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
1799

被折叠的 条评论
为什么被折叠?



