Browse Source

first commit

main
hellodk 2 years ago
parent
commit
c10a16397c
  1. 14
      .gitignore
  2. 9
      Dockerfile
  3. 64
      pom.xml
  4. 154
      src/main/java/MainService.java

14
.gitignore

@ -0,0 +1,14 @@
# ---> Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
.idea

9
Dockerfile

@ -0,0 +1,9 @@
FROM openjdk:11.0.15-jre-bullseye
MAINTAINER hellodk<[email protected]>
WORKDIR /opt/app/
COPY target/tinyimage-1.0-SNAPSHOT-jar-with-dependencies.jar ./
RUN echo 'alias tiny="java -jar /opt/app/tinyimage-1.0-SNAPSHOT-jar-with-dependencies.jar"' >> ~/.bashrc

64
pom.xml

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hellodk</groupId>
<artifactId>tinyimage</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.tinify</groupId>
<artifactId>tinify</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.7.19</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<!-- 运行 jar 包时运行的主类,要求写全类名 需要包含 package name -->
<mainClass>MainService</mainClass>
<!-- 是否指定项目 classpath 下的依赖 -->
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

154
src/main/java/MainService.java

@ -0,0 +1,154 @@
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.swing.clipboard.ImageSelection;
import com.tinify.Source;
import com.tinify.Tinify;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
/**
* @author: hellodk
* @description main service
* @date: 2022/7/19 11:10
*/
public class MainService {
private static final String tmp_folder = "./tmp/";
public static void main(String[] args) {
Instant start = Instant.now();
MainService main = new MainService();
main.getSysClipboard();
Instant end = Instant.now();
Duration between = Duration.between(start, end);
System.out.println("Time elapsed: " + between.getSeconds() + "s.");
System.exit(0);
}
private void setSysClipboard(Object obj) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (obj instanceof String) {
StringSelection stringSelection = new StringSelection((String) obj);
clipboard.setContents(stringSelection, null);
}
else if (obj instanceof Image) {
ImageSelection imageSelection = new ImageSelection((Image) obj);
clipboard.setContents(imageSelection, null);
}
else {
System.out.println("Clipboard content wrong.");
}
}
private void getSysClipboard() {
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable tr = clip.getContents(null);
if (tr != null) {
if (tr.isDataFlavorSupported(DataFlavor.imageFlavor)) {
try {
Image image = (Image) tr.getTransferData(DataFlavor.imageFlavor);
// Create a buffered image with transparency
BufferedImage bImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bImage.createGraphics();
bGr.drawImage(image, 0, 0, null);
bGr.dispose();
File folder = new File(tmp_folder);
folder.mkdir();
String filePath = tmp_folder.concat(getNowTimeStr()).concat(".png");
File output = new File(filePath);
ImgUtil.write(bImage, output);
System.out.println("Original image file name is " + output.getName() + ". File size is " + getFileSize(filePath));
// 调用 tinypng api 去压缩图片并下载下来
tinyPng(output);
}
catch (UnsupportedFlavorException | IOException e) {
System.out.println(e);
}
}
else {
System.out.println("Only deal with image, please put an image to your system clipboard.");
}
}
else {
System.out.println("System clipboard is null!");
}
}
private String getFileSize(String filePath) {
double size = 0;
try {
size = Files.size(Paths.get(filePath));
}
catch (IOException e) {
System.out.println("file not found: " + e);
}
String fileSize;
if (size >= (1024 * 1024)) {
fileSize = String.format("%,.2f MB.", size / 1024 / 1024);
}
else if (size >= 1024) {
fileSize = String.format("%,.2f KB.", size / 1024);
}
else {
fileSize = String.format("%,.2f Bytes.", size);
}
return fileSize;
}
private void tinyPng(File output) {
try {
Tinify.setKey("replace_with_your_tinypng.com_api_key");
byte[] bytes = IoUtil.readBytes(new FileInputStream(output));
Source source = Tinify.fromBuffer(bytes);
String name = output.getName();
String compressedFileName = name.replace(".png", "-compressed.png");
byte[] compressedBytes = source.toBuffer();
String compressedFilePath = tmp_folder.concat(compressedFileName);
File compressedFile = new File(compressedFilePath);
Image newImage = ImageIO.read(new ByteArrayInputStream(compressedBytes));
ImgUtil.write(newImage, compressedFile);
int compressionsThisMonth = Tinify.compressionCount();
System.out.println("Compressions this month: " + compressionsThisMonth + "/500");
System.out.println("Compressed image file name is " + compressedFileName + ". File size is " + getFileSize(compressedFilePath));
// write to system clipboard
setSysClipboard(newImage);
}
catch (IOException e) {
System.out.println(e);
}
}
private String getNowTimeStr() {
LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH-mm-ss-SSS");
String date = now.toLocalDate().format(dateFormatter);
String time = now.toLocalTime().format(timeFormatter);
return date.concat("_").concat(time);
}
}
Loading…
Cancel
Save