Skip to content

Commit e436eae

Browse files
committed
#mist Intelligent auto print mode choice app
1 parent ad34e9d commit e436eae

8 files changed

Lines changed: 792 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@
2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
2424
replay_pid*
25+
26+
target/
27+
.idea/

1.png

20.9 KB
Loading

AutoPrintModeChoice.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from PIL import Image
2+
3+
4+
def is_color_image(url):
5+
img = Image.open(url)
6+
im = img.resize((200, 200))
7+
im.save(url + ".png", format='png')
8+
pix = im.convert('RGB')
9+
width = im.size[0]
10+
height = im.size[1]
11+
oimage_color_type = "Grey Image"
12+
is_color = []
13+
total = 0
14+
colorCnt = 0
15+
for x in range(width):
16+
for y in range(height):
17+
r, g, b = pix.getpixel((x, y))
18+
r = int(r)
19+
g = int(g)
20+
b = int(b)
21+
if (r == g) and (g == b):
22+
pass
23+
elif abs(r - g) < 20 and abs(g - b) < 20 and abs(r - b) < 20:
24+
pass
25+
else:
26+
colorCnt = colorCnt + 1
27+
# print(r,g,b)
28+
oimage_color_type = 'Color Image'
29+
total = total + 1
30+
print("The rate is", colorCnt, total, colorCnt / total)
31+
return oimage_color_type
32+
33+
34+
print(is_color_image(".\\1.png"))

config.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[forceModel]: [color] color mode, [no-color] black-white mode
2+
#
3+
#Sat May 18 22:12:37 CST 2024
4+
printService=HP LaserJet Pro MFP M126nw[C92A81]
5+
prePrintedPaperBox=mainPaperTray
6+
portraitPaperBox=Form-Source
7+
normalPaperBox=mainPaperTray
8+
landscapePaperBox=Form-Source

dependency-reduced-pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.codedoggy</groupId>
5+
<artifactId>AutoPrintModeChoiceApp</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<build>
8+
<plugins>
9+
<plugin>
10+
<artifactId>maven-shade-plugin</artifactId>
11+
<version>3.2.0</version>
12+
<executions>
13+
<execution>
14+
<phase>package</phase>
15+
<goals>
16+
<goal>shade</goal>
17+
</goals>
18+
<configuration>
19+
<filters>
20+
<filter>
21+
<artifact>*:*</artifact>
22+
<excludes>
23+
<exclude>META-INF/*.SF</exclude>
24+
<exclude>META-INF/*.DSA</exclude>
25+
<exclude>META-INF/*.RSA</exclude>
26+
</excludes>
27+
</filter>
28+
</filters>
29+
<transformers>
30+
<transformer>
31+
<mainClass>org.codedoggy.MainInterface</mainClass>
32+
</transformer>
33+
</transformers>
34+
</configuration>
35+
</execution>
36+
</executions>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
<repositories>
41+
<repository>
42+
<id>AsposeJavaAPI</id>
43+
<name>Aspose Java API</name>
44+
<url>https://releases.aspose.com/java/repo/</url>
45+
</repository>
46+
</repositories>
47+
<properties>
48+
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
49+
<maven.compiler.source>1.8</maven.compiler.source>
50+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
51+
<maven.compiler.target>1.8</maven.compiler.target>
52+
</properties>
53+
</project>

pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.codedoggy</groupId>
8+
<artifactId>AutoPrintModeChoiceApp</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.aspose</groupId>
14+
<artifactId>aspose-words</artifactId>
15+
<version>21.6</version>
16+
<classifier>jdk17</classifier>
17+
</dependency>
18+
</dependencies>
19+
20+
<repositories>
21+
<repository>
22+
<id>AsposeJavaAPI</id>
23+
<name>Aspose Java API</name>
24+
<url>https://releases.aspose.com/java/repo/</url>
25+
</repository>
26+
</repositories>
27+
28+
<properties>
29+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30+
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
31+
<maven.compiler.source>1.8</maven.compiler.source>
32+
<maven.compiler.target>1.8</maven.compiler.target>
33+
</properties>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-shade-plugin</artifactId>
40+
<version>3.2.0</version>
41+
<executions>
42+
<execution>
43+
<phase>package</phase>
44+
<goals>
45+
<goal>shade</goal>
46+
</goals>
47+
<configuration>
48+
<filters>
49+
<filter>
50+
<artifact>*:*</artifact>
51+
<excludes>
52+
<exclude>META-INF/*.SF</exclude>
53+
<exclude>META-INF/*.DSA</exclude>
54+
<exclude>META-INF/*.RSA</exclude>
55+
</excludes>
56+
</filter>
57+
</filters>
58+
<transformers>
59+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
60+
<mainClass>org.codedoggy.MainInterface</mainClass>
61+
</transformer>
62+
</transformers>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
</project>
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package org.codedoggy;
2+
3+
import com.aspose.words.Document;
4+
import com.aspose.words.HeaderFooter;
5+
import com.aspose.words.NodeCollection;
6+
import com.aspose.words.NodeType;
7+
import com.aspose.words.PageInfo;
8+
import com.aspose.words.Section;
9+
import com.aspose.words.Shape;
10+
import java.awt.image.BufferedImage;
11+
import java.io.File;
12+
import java.io.FileInputStream;
13+
import java.io.IOException;
14+
import java.io.InputStreamReader;
15+
import java.io.OutputStream;
16+
import java.io.OutputStreamWriter;
17+
import java.lang.reflect.Modifier;
18+
import java.nio.charset.StandardCharsets;
19+
import java.nio.file.Files;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
import java.util.Optional;
23+
import java.util.Properties;
24+
import javax.imageio.ImageIO;
25+
import javax.print.PrintService;
26+
import javax.print.attribute.AttributeSet;
27+
import javax.print.attribute.HashAttributeSet;
28+
import javax.print.attribute.standard.Chromaticity;
29+
import javax.print.attribute.standard.Copies;
30+
import javax.print.attribute.standard.Media;
31+
import javax.print.attribute.standard.PrinterName;
32+
33+
class AppController
34+
{
35+
36+
private static final String CONFIG_FILE_NAME = "config.properties";
37+
38+
private static final String TEMP_DIRECTORY = "temp";
39+
40+
private static final String configComment = "[forceModel]: [color] color mode, [no-color] black-white mode\n";
41+
42+
private String rootPath;
43+
44+
private Properties properties;
45+
46+
private MainInterface mainInterface;
47+
48+
static {
49+
try {
50+
Class<?> aClass = Class.forName("com.aspose.words.zzXyu");
51+
java.lang.reflect.Field zzYAC = aClass.getDeclaredField("zzZXG");
52+
zzYAC.setAccessible(true);
53+
54+
java.lang.reflect.Field modifiersField = zzYAC.getClass().getDeclaredField("modifiers");
55+
modifiersField.setAccessible(true);
56+
modifiersField.setInt(zzYAC, zzYAC.getModifiers() & ~Modifier.FINAL);
57+
zzYAC.set(null, new byte[] { 76, 73, 67, 69, 78, 83, 69, 68 });
58+
} catch (IllegalAccessException | ClassNotFoundException | NoSuchFieldException e) {
59+
e.printStackTrace();
60+
}
61+
}
62+
63+
AppController(MainInterface mainInterface)
64+
{
65+
this.mainInterface = mainInterface;
66+
// read root path
67+
this.rootPath = System.getProperty("user.dir");
68+
69+
this.properties = new Properties();
70+
}
71+
72+
void smartPrint(String filePath, PrintService printService, Media portraitPaperBox, Media landscapePaperBox, int copies) throws Exception
73+
{
74+
// load the file
75+
Document doc = new Document(filePath);
76+
int pageCount = doc.getPageCount();
77+
Map<Document, AttributeSet> printJobs = new LinkedHashMap<>();
78+
for (int page = 0; page < pageCount; page++) {
79+
// get printer properties
80+
HashAttributeSet attributeSet = new HashAttributeSet();
81+
Document extractedPage = doc.extractPages(page, 1);
82+
PageInfo pageInfo = extractedPage.getPageInfo(0);
83+
boolean isLandscape = pageInfo.getLandscape();
84+
if (!isLandscape) {
85+
// remove the header and footer
86+
for (Section section : extractedPage.getSections()) {
87+
for (HeaderFooter headerFooter : section.getHeadersFooters()) {
88+
NodeCollection<Shape> shapes = headerFooter.getChildNodes(NodeType.SHAPE, true);
89+
for (Shape shape : shapes) {
90+
if (shape.hasImage()) {
91+
shape.remove();
92+
}
93+
}
94+
}
95+
}
96+
}
97+
98+
String tempFilePath = getTempFilePath() + File.separator + doc.hashCode() + "_page_" + (page + 1) + ".docx";
99+
extractedPage.save(tempFilePath);
100+
Document extractedDoc = new Document(tempFilePath);
101+
boolean isColor = isLandscape || isColorImage(extractedDoc);
102+
// 检查配置
103+
String forceModel = (String) properties.get("forceModel");
104+
if (forceModel != null) {
105+
// 强制模式
106+
if (forceModel.equals("color")) {
107+
isColor = true;
108+
}
109+
if (forceModel.equals("no-color")) {
110+
isColor = false;
111+
}
112+
}
113+
114+
mainInterface.printMessage(
115+
"document page " + (page + 1) + (isLandscape ? " landscape " : " portrait ")
116+
+ ", " + (isColor ? "color mode" : "black-white mode")
117+
+ "\n");
118+
119+
attributeSet.add(new PrinterName(printService.getName(), null));
120+
121+
attributeSet.add(new Copies(1));
122+
123+
Optional.ofNullable(isLandscape ? landscapePaperBox : portraitPaperBox).ifPresent(attributeSet::add);
124+
125+
attributeSet.add(isColor ? Chromaticity.COLOR : Chromaticity.MONOCHROME);
126+
127+
// extractedDoc.print(attributeSet);
128+
printJobs.put(extractedDoc, attributeSet);
129+
}
130+
131+
mainInterface.printMessage("Processing completed, send the file to printer...\n");
132+
for (int i = 0; i < copies; i++) {
133+
for (Map.Entry<Document, AttributeSet> entry : printJobs.entrySet()) {
134+
entry.getKey().print(entry.getValue());
135+
}
136+
}
137+
}
138+
139+
private String getTempFilePath()
140+
{
141+
return rootPath + File.separator + TEMP_DIRECTORY;
142+
}
143+
144+
private boolean isColorImage(Document document) throws Exception
145+
{
146+
String tempPngFilePath = getTempFilePath() + File.separator + document.hashCode() + ".png";
147+
document.save(tempPngFilePath);
148+
149+
BufferedImage bufferedImage = ImageIO.read(new File(tempPngFilePath));
150+
151+
int[][][] result = null;
152+
int threshold = 20;
153+
154+
int height = bufferedImage.getHeight();
155+
int width = bufferedImage.getWidth();
156+
result = new int[height][width][3];
157+
for (int i = 0; i < height; i++) {
158+
for (int j = 0; j < width; j++) {
159+
int rgb = bufferedImage.getRGB(j, i);//visiting sequence for image: column, row
160+
result[i][j][0] = (rgb & 0xff0000) >> 16;
161+
result[i][j][1] = (rgb & 0xff00) >> 8;
162+
result[i][j][2] = (rgb & 0xff);
163+
164+
if (Math.abs(result[i][j][0] - result[i][j][1]) <= threshold && Math.abs(result[i][j][1] - result[i][j][2]) <= threshold
165+
&& Math.abs(result[i][j][2] - result[i][j][0]) <= threshold) {
166+
167+
} else {
168+
return true;
169+
}
170+
}
171+
}
172+
173+
return false;
174+
}
175+
176+
Properties readConfig() throws IOException
177+
{
178+
String configPath = rootPath + File.separator + CONFIG_FILE_NAME;
179+
File configFile = new File(configPath);
180+
if (!configFile.exists()) {
181+
configFile.createNewFile();
182+
}
183+
184+
FileInputStream input = new FileInputStream(configFile);
185+
properties.load(new InputStreamReader(input, StandardCharsets.UTF_8));
186+
return properties;
187+
}
188+
189+
void saveConfig() throws IOException
190+
{
191+
String configPath = rootPath + File.separator + CONFIG_FILE_NAME;
192+
File configFile = new File(configPath);
193+
if (!configFile.exists()) {
194+
configFile.createNewFile();
195+
}
196+
197+
OutputStream os = Files.newOutputStream(configFile.toPath());
198+
OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
199+
properties.store(osw, configComment);
200+
}
201+
202+
void cleanTemp()
203+
{
204+
String tempPath = getTempFilePath();
205+
File tempDir = new File(tempPath);
206+
if (tempDir.exists()) {
207+
File[] files = tempDir.listFiles();
208+
for (File file : files) {
209+
file.delete();
210+
}
211+
}
212+
}
213+
214+
void savePrinterState(PrintService printService, Media portraitPaperBox, Media landscapePaperBox)
215+
{
216+
properties.put("printService", printService.getName());
217+
properties.put("portraitPaperBox", String.valueOf(portraitPaperBox));
218+
properties.put("landscapePaperBox", String.valueOf(landscapePaperBox));
219+
}
220+
}

0 commit comments

Comments
 (0)