import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import javax.imageio.IIOException; import javax.imageio.ImageIO; public class ImageUtils { /** * 对图片进行放大或者压缩 * * @param srcPath * 原始图片路径(绝对路径) * @param newPath * 处理后图片路径(绝对路径) * @param w * 目标宽度 * @param h * 目标高度 * @return 是否处理成功 */ public static boolean zoomInImage(String srcPath, String newPath, Integer w, Integer h) { BufferedImage bufferedImage = null; try { File file = new File(srcPath); if (file.canRead()) { try{ bufferedImage = ImageIO.read(file); }catch(IIOException e){ e.printStackTrace(); return false; } } else { System.out.println("文件不存在或没有权限读取:" + srcPath); return false; } } catch (IOException e) { // TODO: 打印日志 e.printStackTrace(); return false; } if (bufferedImage != null) { bufferedImage = zoomInImage(bufferedImage, w, h); try { // 保存修改后的图像,全部保存为JPG格式 ImageIO.write(bufferedImage, "JPG", new File(newPath)); System.out.println("压缩后的图片已保存到磁盘"); } catch (IOException e) { // TODO 打印错误信息 e.printStackTrace(); return false; } } return true; } /** * 对图片进行放大或者压缩处理 * * @param originalImage * 原始图片 * @param times * 放大倍数 * @return */ public static BufferedImage zoomInImage(BufferedImage originalImage, Integer width, Integer height) { BufferedImage newImage = new BufferedImage(width, height, originalImage.getType()); Graphics g = newImage.getGraphics(); g.drawImage(originalImage, 0, 0, width, height, null); System.out.println("图片压缩成功"); g.dispose(); return newImage; } /** * 将图片写入到磁盘 * * @param img * 图片数据流 * @param fileName * 文件保存时的名称 */ public static void writeImageToDisk(byte[] img, String fileSrc) { try { File file = new File( fileSrc); FileOutputStream fops = new FileOutputStream(file); fops.write(img); fops.flush(); fops.close(); System.out.println("原图已下载到本地磁盘"); } catch (Exception e) { e.printStackTrace(); } } /** * 根据地址获得数据的字节流 * * @param strUrl * 网络连接地址 * @return */ public static byte[] getImageFromNetByUrl(String strUrl) { try { URL url = new URL(strUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据 byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 从输入流中获取数据 * * @param inStream * 输入流 * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } public static void main(String[] args) { // 图片url,压缩后的宽和高 String url = "/upload/c671c57023e6eb83a1fa492babf0779e.jpg"; int w = 700; int h = 500; //获取图片访问真实url if(url.startsWith("/")){ url = "http://localhost"+url; } String newDir = "/var/imgcompress/"; String newName = MD5Util.encodeMD5(url); //从网络下载图片并保存到本地磁盘 String srcPath = newDir + newName + url.substring(url.indexOf(".")); writeImageToDisk(getImageFromNetByUrl(url),srcPath); //压缩后的图片路径 String filePath = newDir + newName + "_" + w + "_" + h + url.substring(url.indexOf(".")); System.out.println(filePath); // 测试 boolean test = zoomInImage(srcPath, filePath, w, h); if (test) { System.out.println("ok"); } } }
时间: 2024-10-25 18:37:23