byte[] src = roiAbgr.getByteArray(0,w * h * 4);
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
如何用BufferedImage这个类把src 转为jpg图片
代码如下,望采纳
/**
* 获取验证码图片
* @return jpg
*/
@RequestMapping(value = "/getCodeImage")
public void getValidateCodeImage(HttpServletRequest request,HttpServletResponse response) throws IOException {
JSONObject json = new JSONObject();
PrintWriter out = null;
out = response.getWriter();
byte[] src = roiAbgr.getByteArray(0,w * h * 4);
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
//临时图片路径
File file = new File("d:\\image\\" + UUID.randomUUID().toString() + ".jpg");
if(!(file.getParentFile().exists())){ //判断d:\image目录是否存在
file.getParentFile().mkdir();
}
//输出到临时图像
ImageIO.write(image, "jpg", file);//jpg可以换成你想要的图片格式
this.file = file;
json.put("validateCodeImage", file);
if (null != out) {
out.close();
}
}
先把BufferedImage 转为InputStream 流
public InputStream bufferedImageToInputStream(BufferedImage image) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "JPG", os);
return new ByteArrayInputStream(os.toByteArray());
}
然后将InputStream 流写入到本地文件
/**
* 将InputStream写入本地文件
* @param destination 写入本地目录
* @param input 输入流
* @throws IOException IOException
*/
public static void writeToLocal(String destination, InputStream input)
throws IOException {
int index;
byte[] bytes = new byte[1024];
FileOutputStream downloadFile = new FileOutputStream(destination);
while ((index = input.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
input.close();
downloadFile.close();
}
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
public class ResizeImageExample {
public static void main(String... args) throws IOException {
File input = new File("ss-2.png");
BufferedImage image = ImageIO.read(input);
BufferedImage resized = resizebyaspect(image, 1655, 2340);
File output = new File("ss-2-resized-500x500.png");
ImageIO.write(resized, "png", output);
}
// 不按比例缩放
private static BufferedImage resize(BufferedImage img, int height, int width) {
Image tmp = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return resized;
}
// 按比例缩放
private static BufferedImage resizebyaspect(BufferedImage img, int height, int width) {
int ori_width = img.getWidth();
int ori_height = img.getHeight();
float ratio_w = (float)width/ori_width;
float ratio_h = (float)height/ori_height;
int new_width = (ratio_w < ratio_h) ? width : (int)(ratio_h * ori_width);
int new_height = (ratio_h < ratio_w) ? height : (int)(ratio_w * ori_height);
System.out.println(new_height);
System.out.println(new_width);
Image tmp = img.getScaledInstance(new_width, new_height, Image.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(new_width, new_height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return resized;
}
}
说明:
输入的图片可以是各种格式
输出的图片也可以是各种格式, 不同的格式修改下面的"png",为"jpg"或其他
ImageIO.write(resized, "png", output);
若能解决你的问题,帮忙点击一个采纳,十分感谢
byte[] src = roiAbgr.getByteArray(0,w * h * 4);
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
ImageIO.write(image , "jpg", new File("C:/test/result"));
代码如一楼二楼所示,我就不重复了
上网查(手动滑稽)