图片处理

/**
* 缩小或放大图片
* @param data 图片的byte数据
* @param w 需要缩到的宽度
* @param h 需要缩到高度
* @return 缩放后的图片的byte数据
*/
private byte[] ChangeImgSize(byte[] data, int nw, int nh){
byte[] newdata = null;
try{
BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data));
int w = bis.getWidth();
int h = bis.getHeight();
double sx = (double) nw / w;
double sy = (double) nh / h;
AffineTransform transform = new AffineTransform();
transform.setToScale(sx, sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
//原始颜色
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis, bid);

//转换成byte字节
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bid, "jpeg", baos);
newdata = baos.toByteArray();

}catch(IOException e){
e.printStackTrace();
}
return newdata;
}

void buff2Image(byte[] b,String tagSrc) throws Exception
{
try {
FileOutputStream fout = new FileOutputStream(tagSrc);
//将字节写入文件
fout.write(b);
fout.close();
} catch (Exception e) {
// TODO: handle exception
}
}

posted on 2018-03-29 15:26  四海骄阳  阅读(104)  评论(0编辑  收藏  举报

导航