Em share các bác đoạn code giúp nén ảnh còn 1/8 dung lượng so với ban đầu mà chất lượng giữ được 9/10
Kèm chèn logo hoặc text vào ảnh :
File jar cho các bác, máy các bác phải cài java nha
Link file jar: Các bác tải ở dưới bài viết nhé
Cách chạy :
Các bác vào thư mục chứa file jar đã tải
Bật cmd hoặc terminal lên
gõ : java -jar conpressImage.jar + đường dẫn đến thư mục chứa ảnh
Ví dụ: các bác lưu ảnh ở thư mục /home/image:
java -jar conpressImage.jar /home/image/
Ảnh sau khi nén sẽ thay thế ảnh gốc luôn ạ !!!!!!
Ví dụ ảnh các bác ban đầu là 1.6 Mb, sau khi nén chỉ còn tầm 200kb thôi!!!!!
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class mainTextToImage {
public static void main(String[] args) {
String folder="đường dẫn thư mục chứa ảnh";
File dir = new File(folder);
ArrayList<String> names = new ArrayList<String>(Arrays.asList(dir.list()));
System.out.println(names.size());
for(String name:names) {
System.out.println(name);
// chữ cần chèn
String text = "điền chữ cần chèn";
File input = new File(folder+name);
File output = new File(folder+name);
try {
BufferedImage image = ImageIO.read(input);
if (image==null) {
continue;
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
continue;
}
// adding text as overlay to an image
try {
addTextWatermark(text, "jpg", input, output);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
continue;
}
}
// overlay settings
// overlay settings
}
private static void addTextWatermark (String text, String type, File source, File destination) throws Exception {
BufferedImage image = ImageIO.read(source);
// determine image type and handle correct transparency
int imageType = "png".equalsIgnoreCase(type) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
BufferedImage watermarked = new BufferedImage(image.getWidth(), image.getHeight(), imageType);
// initializes necessary graphic properties
Graphics2D w = (Graphics2D) watermarked.getGraphics();
w.drawImage(image, 0, 0, null);
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
w.setComposite(alphaChannel);
w.setColor(Color.GRAY);
w.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 40));
FontMetrics fontMetrics = w.getFontMetrics();
Rectangle2D rect = fontMetrics.getStringBounds(text, w);
// calculate center of the image
int centerX = (image.getWidth() - (int) rect.getWidth()) / 2;
int centerY = image.getHeight() / 2;
// add text overlay to the image
w.drawString(text, centerX, centerY);
ImageIO.write(watermarked, type, destination);
w.dispose();
}
}
Nguồn: cafemmo
[khungdownload manguon=”” redirectbanggi=”sharescript-redirect” tenfilezip=”conpressImage.jar” linkbutton1=”https://drive.google.com/file/d/1VeBI95Tq0k76pt22mzzYl_UZqs1N1np5/view” tenbutton1=”Drive” linkbutton2=”https://drive.google.com/file/d/1VeBI95Tq0k76pt22mzzYl_UZqs1N1np5/view” tenbutton2=”Dự phòng”]