Cafemmo Club
← Quay lại danh sách

Share code nén dung lượng ảnh còn 1/8 và chèn chữ hoặc logo vào ảnh (java) !!!!

thangvbvb2511/09/2021355 lượt xem5 bình luận

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: https://drive. google. com/file/d/1jVdNxhhKlDhETnR2YGo7ufH-UQkbZXPY/view?usp=sharing

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) {</p>
<p>String folder=&quot;đường dẫn thư mục chứa ảnh&quot;;</p>
<p>File dir = new File(folder);
    ArrayList&lt;String&gt; names = new ArrayList&lt;String&gt;(Arrays.asList(dir.list()));
    System.out.println(names.size());</p>
<p>for(String name:names) {</p>
<p>System.out.println(name);
    // chữ cần chèn
      String text = &quot;điền chữ cần chèn&quot;;
        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;
        }</p>
<p>// adding text as overlay to an image
        try {
            addTextWatermark(text, &quot;jpg&quot;, input, output);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            continue;
        }
}
    // overlay settings</p>
<p>// overlay settings
}</p>
<p>private static void addTextWatermark (String text, String type, File source, File destination) throws Exception {
    BufferedImage image = ImageIO.read(source);</p>
<p>// determine image type and handle correct transparency
    int imageType = &quot;png&quot;.equalsIgnoreCase(type) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
    BufferedImage watermarked = new BufferedImage(image.getWidth(), image.getHeight(), imageType);</p>
<p>// 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);</p>
<p>// calculate center of the image
    int centerX = (image.getWidth() - (int) rect.getWidth()) / 2;
    int centerY = image.getHeight() / 2;</p>
<p>// add text overlay to the image
    w.drawString(text, centerX, centerY);
    ImageIO.write(watermarked, type, destination);
    w.dispose();
}
}

Bình luận

Đang tải...

Đang kiểm tra đăng nhập...

Share code nén dung lượng ảnh còn 1/8 và chèn chữ hoặc logo vào ảnh (java) !!!! · Cafemmo Club