Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation method of batch compression of pixels in Java images?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Java picture batch compression of pixel implementation method is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Great method of picture compression

In order to prevent the loss of user traffic, even in the case that 5g is coming, the compression algorithm is still very necessary. I'm sorry, what is not introduced today is the compression algorithm? Mainly talk about how to compress the picture through java and control the compression loss ratio as much as possible, not only to reduce storage, but also to present it to users quickly. Only a good experience can reduce the loss of traffic in this impatient era.

Recently, because the company needs xxx certification to upload a specific screenshot of the test case function, and found that there is a size limit, so the image compression, a simple record.

Pre-compression size:

Compressed size:

Specific code implementation:

Main method test:

Public static void main (String [] args) throws IOException {String modpath = "C:\\ Users\\ Administrator\\ Desktop\\ Kunpeng Certification\\ test\\"; getFiles ("C:\\ Users\\ Administrator\\ Desktop\\ Kunpeng Certification\\ Test case list", modpath, 160); / / compress the image to 100W}

File size processing

/ * * @ param srcPath original picture path * @ param desPath converted picture path * @ param width converted picture width * @ param height converted picture height * / public static void resizeImage (String srcPath, String desPath, int width, int height) throws IOException {File srcFile = new File (srcPath); Image srcImg = ImageIO.read (srcFile); BufferedImage buffImg = null BuffImg = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); / / the image modified with TYPE_INT_RGB will change color buffImg.getGraphics (). DrawImage (srcImg.getScaledInstance (width, height, Image.SCALE_SMOOTH), 0,0, null); String filePath= ""; if (srcFile.getName (). Contains ("#")) {filePath= srcFile.getName (). Replace ("#", ") } else {filePath=srcFile.getName ();} ImageIO.write (buffImg, "PNG", new File (desPath + filePath);}

Get directory file information

/ * * @ param scaleSize image modification ratio, target width * / public static void getFiles (String path, String modPath, int scaleSize) throws IOException {ArrayList files = new ArrayList (); File file = new File (path); File [] tempList = file.listFiles (); / / cycle through for (int I = 0; I) in the directory

< tempList.length; i++) { String filePath = tempList[i].getName(); if (tempList[i].isFile()) { System.out.println("文件:" + filePath + "-" + tempList[i].getAbsolutePath().replaceAll("\\\\", "/")); String[] imagePath = tempList[i].getAbsolutePath().replaceAll("\\\\", "/").split("/"); String imageNumber = null; FileUtil.resizeImage(tempList[i].getAbsolutePath().replaceAll("\\\\", "/"), modPath, 160, 160); files.add(tempList[i].toString()); } if (tempList[i].isDirectory()) { System.out.println("文件夹:" + tempList[i]); } } System.out.println(path + "下文件数量:" + files.size()); } 控制台目录压缩成功保存到盘符: 附:利用Graphics类如何进行压缩图像 Graphics类提供基本绘图方法,Graphics类提供基本的几何图形绘制方法,主要有:画线段、画矩形、画圆、画带颜色的图形、画椭圆、画圆弧、画多边形、画字符串等。 这里不做一一赘述, 进重点介绍一下,利用Graphics类如何进行压缩图像。不多说直接上代码。 /** * compressImage * * @param imageByte * Image source array * @param ppi * @return */ public static byte[] compressImage(byte[] imageByte, int ppi) { byte[] smallImage = null; int width = 0, height = 0; if (imageByte == null) return null; ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte); try { Image image = ImageIO.read(byteInput); int w = image.getWidth(null); int h = image.getHeight(null); // adjust weight and height to avoid image distortion double scale = 0; scale = Math.min((float) ppi / w, (float) ppi / h); width = (int) (w * scale); width -= width % 4; height = (int) (h * scale); if (scale >

= (double) 1) return imageByte; BufferedImage buffImg = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); buffImg.getGraphics () .drawImage (image.getScaledInstance (width, height, Image.SCALE_SMOOTH), 0,0, null); ByteArrayOutputStream out = new ByteArrayOutputStream () ImageIO.write (buffImg, "png", out); smallImage = out.toByteArray (); return smallImage;} catch (IOException e) {log.error (e.getMessage ()); throw new RSServerInternalException (") }}

In fact, there are only two key points.

BufferedImage buffImg = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); buffImg.getGraphics () .drawImage (image.getScaledInstance (width, height, Image.SCALE_SMOOTH), 0,0, null); is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development