/** * 对文件进行压缩,调用方法 * @throws Exception * @author wud * @param path(图片的全路径) * */ public static void compressPicture(String path) throws Exception{ if (StringUtil.isNullOrEmpty(path)) { throw new BaseException("上传图片路径错误"); } String format = FileUtil.getFilename(path); File file=new File(path); float flt=0.7f; int wh=1024; dealPictures(file,format,wh,flt,path); } /** * 文件压缩与缩放 * @param file 文件路径(file) * @param format 文件格式 * @param wh 高宽最大尺寸 * @param per 百分比 * @param path 文件路径(String) * * * * * */ public static void dealPictures(File file,String format,int wh,float per,String path){ Image src; try { src=ImageIO.read(file); //构造Image对象 int old_w= src.getWidth(null); //源图宽 int old_h= src.getHeight(null); Image newIm = null; //定义处理后的Image if (old_w>1024 || old_h>1024) { if(old_w>old_h){ newIm = src.getScaledInstance(wh, (wh * old_h) / old_w, Image.SCALE_SMOOTH); } else{ newIm = src.getScaledInstance((wh * old_w) / old_h, wh, Image.SCALE_SMOOTH); } Image temp = new ImageIcon(newIm).getImage(); //获取图片 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics g = bufferedImage.createGraphics(); //获取绘图对象 g.setColor(Color.white); g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); //绘制背景。 g.drawImage(temp, 0, 0, null); g.dispose(); /*BufferedImage image_to_save = new BufferedImage(temp.getWidth(null), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);*/ bufferedImage.getGraphics().drawImage(src.getScaledInstance(temp.getWidth(null), temp.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream fos; fos = new FileOutputStream(path); //新方法 saveAsJPEG(100, bufferedImage, per, fos,format); fos.close(); } //高宽都小于1024 else{ BufferedImage bi2 = new BufferedImage(old_w, old_h, BufferedImage.TYPE_INT_RGB); //创建图像上下文的抽象类 Graphics2D g2d = bi2.createGraphics(); // 绘制指定图像中当前可用的图像。 g2d.drawImage(src, 0, 0, null); // 释放此图形的上下文以及它使用的所有系统资源 g2d.dispose(); //数据输出 ImageIO.write(bi2,format,file); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } public static void saveAsJPEG(Integer dpi, BufferedImage image_to_save, float JPEGcompression, FileOutputStream fos,String format) throws IOException { ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();//此处格式“jpg”是固定的 ImageOutputStream ios = ImageIO.createImageOutputStream(fos); imageWriter.setOutput(ios); //and metadata IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image_to_save), null); if (dpi != null && !dpi.equals("")) { //new metadata Element tree = (Element) imageMetaData.getAsTree("javax_imageio_jpeg_image_1.0"); Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0); jfif.setAttribute("Xdensity", Integer.toString(dpi)); jfif.setAttribute("Ydensity", Integer.toString(dpi)); } if (JPEGcompression >= 0 && JPEGcompression <= 1f) { JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam(); jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); jpegParams.setCompressionQuality(JPEGcompression); } //old write and clean //jpegEncoder.encode(image_to_save, jpegEncodeParam); //new Write and clean up imageWriter.write(imageMetaData, new IIOImage(image_to_save, null, null), null); ios.close(); imageWriter.dispose(); }
公司内局域网中的redis缓存和sock线程池,不在局域内运行程序怎么处理
最新推荐文章于 2023-05-17 10:17:59 发布