/**
* 将原图压缩生成jpeg格式的数据
* @param source
* @return
*/
public static byte[] wirteJPEGBytes(BufferedImage source){
if(null==source)
throw new NullPointerException();
ByteArrayOutputStream output = new ByteArrayOutputStream();
JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(output);
JPEGEncodeParam param = jencoder.getDefaultJPEGEncodeParam(source);
param.setQuality(0.75f, true);
jencoder.setJPEGEncodeParam(param);
try {
jencoder.encode(source);
} catch (ImageFormatException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return output.toByteArray();
}
/**
* 将原图压缩生成jpeg格式的数据
* @param source
* @return
* @see #wirteBytes(BufferedImage, String)
*/
public static byte[] wirteJPEGBytes(BufferedImage source){
return wirteBytes(source,"JPEG");
}
/**
* 将原图压缩生成jpeg格式的数据
* @param source
* @return
* @see #wirteBytes(BufferedImage, String)
*/
public static byte[] wirteJPEGBytes(BufferedImage source){
return wirteBytes(source,"JPEG");
}
/**
* 将{@link BufferedImage}生成formatName指定格式的图像数据
* @param source
* @param formatName 图像格式名,图像格式名错误则抛出异常
* @return
*/
public static byte[] wirteBytes(BufferedImage source,String formatName){
Assert.notNull(source, "source");
Assert.notEmpty(formatName, "formatName");
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
if(!ImageIO.write(source, formatName.toLowerCase(), output))
// 返回false则抛出异常
throw new IllegalArgumentException(String.format("not found writer for '%s'",formatName));
} catch (IOException e) {
throw new RuntimeException(e);
}
return output.toByteArray();
}
/**
* 将{@link BufferedImage}生成formatName指定格式的图像数据
* @param source
* @param formatName 图像格式名,图像格式名错误则抛出异常
* @return
*/
public static byte[] wirteBytes(BufferedImage source,String formatName){
Assert.notNull(source, "source");
Assert.notEmpty(formatName, "formatName");
ByteArrayOutputStream output = new ByteArrayOutputStream();
BufferedImage newBufferedImage = new BufferedImage(source.getWidth(),
source.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = newBufferedImage.createGraphics();
try {
g.drawImage(source, 0, 0,null);
if(!ImageIO.write(newBufferedImage, formatName, output))
throw new IllegalArgumentException(String.format("not found writer for '%s'",formatName));
} catch (IOException e) {
throw new RuntimeException(e);
}finally{
g.dispose();
}
return output.toByteArray();
}
/**
* 将{@link BufferedImage}生成formatName指定格式的图像数据
* @param source
* @param formatName 图像格式名,图像格式名错误则抛出异常
* @return
*/
public static byte[] wirteBytes(BufferedImage source,String formatName){
Assert.notNull(source, "source");
Assert.notEmpty(formatName, "formatName");
ByteArrayOutputStream output = new ByteArrayOutputStream();
Graphics2D g = null;
try {
for(BufferedImage s=source;!ImageIO.write(s, formatName, output);){
if(null!=g)
throw new IllegalArgumentException(String.format("not found writer for '%s'",formatName));
s = new BufferedImage(source.getWidth(),
source.getHeight(), BufferedImage.TYPE_INT_RGB);
g = s.createGraphics();
g.drawImage(source, 0, 0,null);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (null != g)
g.dispose();
}
return output.toByteArray();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有