解决方案 »
- java怎么建立一对多的消息发放功能
- 获取有关客户端上次请求的URL的常规方案
- 求救啊 ~~中关数据交换服务器CenDXS 安装配置
- 困扰了一个多月的问题了,看看有没有高人能帮我解决下。
- struts2.0 中如何自定义标签
- 请问谁有读取excel的包java excelAPI
- 从数据库提取数据和struts的JSP得到数据做比较时,是否需要转码?急!
- JOC混淆器的用法
- j2ee tutorial困惑,session bean与entity bean
- 那位gg有关于ejb的电子文档,多谢了
- Spring整合Hibernate,取不到hibernateTemplate的值,求助!
- 求算法,找零钱
@SuppressWarnings("finally")
public static byte[] scale(byte[] bytes , double width, double height) {
BufferedImage bufferedImage = null;
BufferedImage bufTarget = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
bufferedImage = ImageIO.read(bais);
double sx = width / bufferedImage.getWidth();
double sy = height / bufferedImage.getHeight();
int type = bufferedImage.getType();
if (type == BufferedImage.TYPE_CUSTOM) {
ColorModel cm = bufferedImage.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster((int)width, (int)height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else {
bufTarget = new BufferedImage((int)width, (int)height, type);
}
Graphics2D g = bufTarget.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(bufferedImage, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
if(bufTarget != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufTarget, "jpeg", baos);
byte[] result = baos.toByteArray();
return result;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
图片压缩,把图片的byte字节和需要重新生成的大小传递进来即可.