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

How to realize the function of picture compression in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, Xiaobian will bring you about how to realize the image compression function in Java. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

/** isoscale compressed original * @param url * Picture Network Address * @ pardestam Width Width to compress * @ pardestam Height Height to compress * @param outputDir Output Directory * @param filename File name **/ public static String thumbnail(URL url, int destWidth, int destHeight,String outputDir,String filename){ System.out.println("Image compression start"); long startTime = System.currentTimeMillis(); String returnurl =""; //compressed image location try { //read original BufferedImage BI = ImageIO.read(url); int srcWidth = BI.getWidth(); //original width int srcHeight = BI.getHeight(); //original height returnurl = outputDir+File.separatorChar+filename; OutputStream fos =new FileOutputStream(returnurl); if(srcWidth>=destWidth || srcHeight>=destWidth) { double sx = (double) destWidth / srcWidth; double sy = (double) destHeight / srcHeight; //scaling if (sx > sy) { sx = sy; destWidth = (int) (sx * srcWidth); } else { sy = sx; destHeight = (int) (sy * srcHeight); } } //SCALE_SMOOTH compression algorithm may have some impact on picture sharpness, but it is still within acceptable range Image image = BI.getScaledInstance(destWidth, destHeight, Image.SCALE_SMOOTH); BufferedImage tag = new BufferedImage(destWidth, destHeight,BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.setColor(Color.RED); g.drawImage(image, 0, 0, null); //Draw the processed graph g.dispose(); ImageIO.write(tag, "JPEG", fos); System.out.println("Image compression ends"); long endTime = System.currentTimeMillis(); System.out.println("Total Image Compression Time: " +(endTime-startTime)+"ms" ); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return returnurl; } The above is how to implement the image compression function in Java shared by Xiaobian. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report