java-png picture compression, picture compression png solve the problem of black background

There is a demand that the project you want customers to upload the picture compression, the requirement is to make the image smaller size, but the resolution is unchanged.

There are mainstream online two ways, one is the use of java native BufferImage to compress, and the second is provided by Google Thumbnails compression, two methods for other formats other than png picture of the processing is not a problem, but for the png image, due to the support of irregular png picture, transparent background problems, leading to the compressed photo black background, said the internet can be the same black background fashion background after treatment is not black but picture resolution is reduced. This is mainly the.

Then find a fee Api, Tinypng . I believe that many students studied png compression have seen the software, the software can meet the requirements, however, he is charged, the monthly limit the number of compressed 500 times, excess charges. Also there are a Jdeli software, he also provides a java Api, but the same charges.

Then I found a product called pngquant open source software, but it is c development, java want to call, then it can only be so labeled or dll libraries, through jni call, I will not speak a lot of students did not contact jni even without considering the issue of implementation difficulty, he still has a very serious problem is that of so labeled Java.library.path library needs to be put under, which means that when you want to deploy on a production server, you need to change production configuration server. And our project, because the business needs, the project is also deployed on a different server, a proxy server, there are already more than 50, and if the costs in this way, it is undoubtedly a significant increase in project deployment.

And then found out that the above Tinypng and Jdeli Both charges are based on pngquant software open source project based on the development, people very silent, and there seems to have opportunities?

After studying for a long time, and he found a pdf open source software rendering OpenViewerFX , but this software which provides a picture compression Png interface. The same is pngquant the same algorithm as the basis to achieve the effect of several software above, but the important thing is that it is open source! He is open source! It is open source!

And he is the java language development, provides a jar package, open interface png image processing. Here to say how png image compression by this software.

github Address:

https://github.com/qwertme/OpenViewerFX

maven Quote:

<dependency>
    <groupId>org.jpedal</groupId>
    <artifactId>OpenViewerFX</artifactId>
    <version>6.6.14</version>
</dependency>

Demo:

import com.idrsolutions.image.png.PngCompressor;
import java.io.File;
public class PngTest {
public static void main(String[] args) throws Exception{
String filePath = "C:\\Users\\Owner\\Desktop\\图片素材\\test1.png";
File file = new File(filePath);
File outfile=new File("C:\\Users\\Owner\\Desktop\\图片素材\\test11.png");
PngCompressor.compress(file,outfile);
}
}

The output comparison:

 

 

 

 

 

 

 

You can see the background picture is not black, size did not shrink, clarity remains good. The perfect solution for all problems.

OK, that is so simple, the effect is perfect.

Guess you like

Origin www.cnblogs.com/yinjing/p/12157562.html