Merge branch 'dev' into 'master'

Dev

See merge request namophice/ink-photo-album!1
This commit is contained in:
2023-03-14 09:38:58 +08:00
9 changed files with 141 additions and 122 deletions

4
.gitignore vendored
View File

@@ -16,4 +16,6 @@ gradle-app.setting
.idea/
cache
error.log
gradle/
gradle/
target
logs

View File

@@ -4,15 +4,16 @@
用Java编写的基于树莓派的电子墨水屏相册
#### 效果图
![效果图1](https://images.gitee.com/uploads/images/2021/0807/125215_b6d4fd2e_7404037.jpeg "微信图片_20210807125140.jpg")
![效果图1](https://lsky-pro.namophice.icu/uHPUfu.jpg "微信图片_20210807125140.jpg")
![效果图2](https://images.gitee.com/uploads/images/2021/0807/125242_cfcada7d_7404037.jpeg "微信图片_20210807125134.jpg")
![效果图2](https://lsky-pro.namophice.icu/fijIGo.jpg "微信图片_20210807125134.jpg")
![效果图3](https://images.gitee.com/uploads/images/2021/0807/125303_bedcd72e_7404037.jpeg "微信图片_20210807125148.jpg")
![效果图3](https://lsky-pro.namophice.icu/0omx8h.jpg "微信图片_20210807125148.jpg")
#### 相关链接
https://gitee.com/namophice/ink-photo-album
http://gitlab.namophice.icu/namophice/ink-photo-album.git
https://github.com/Namophice/ink-photo-album
https://gitee.com/namophice/ink-photo-album
https://zhuanlan.zhihu.com/p/397304172
#### PS

View File

@@ -1,33 +0,0 @@
plugins {
id 'java-library'
id 'com.github.johnrengelman.shadow' version '6.1.0'
}
group 'icu.namophice'
version '1.0-SNAPSHOT'
description = 'slow'
sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.76'
// RaspbarryPi驱动接口核心包
implementation group: 'com.pi4j', name: 'pi4j-core', version: '1.4'
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
jar {
manifest {
attributes(
'Main-Class': 'icu.namophice.inkphotoalbum.Main'
)
}
}

60
pom.xml Normal file
View File

@@ -0,0 +1,60 @@
<?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>icu.namophice</groupId>
<artifactId>ink-photo-album</artifactId>
<version>1.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.23</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
icu.namophice.inkphotoalbum.Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,2 +0,0 @@
rootProject.name = 'ink-photo-album'

View File

@@ -17,9 +17,6 @@ public class Main {
// 初始化配置
DefaultConfig.initConfig();
// 从cache文件获取imageIndex
DefaultConfig.initImageIndexWithCache();
// 进入主流程
MasterService.master();
} catch (Exception e) {

View File

@@ -5,15 +5,16 @@ import icu.namophice.inkphotoalbum.driver.EPaper;
import icu.namophice.inkphotoalbum.utils.CommonUtil;
import icu.namophice.inkphotoalbum.utils.ImageUtil;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Namophice
@@ -21,19 +22,19 @@ import java.util.regex.Pattern;
*/
public class MasterService {
public static void master() throws IOException, InterruptedException {
public static void master() throws Exception {
CommonUtil.printLogToConsole("The master program is running ...");
final EPaper ePaper = EPaper.getInstance();
ePaper.init();
// 从cache文件获取imageIndex
DefaultConfig.initImageIndexWithCache();
if (DefaultConfig.imageIndex < 0) {
DefaultConfig.imageIndex = 0;
}
EPaper ePaper = EPaper.getInstance();
ePaper.init();
if (DefaultConfig.imageIndex == 0) {
BufferedImage defaultImage = ImageUtil.getImageToScreen();
CommonUtil.printLogToConsole("Print images to screen ...");
ePaper.drawImage(defaultImage);
}
@@ -44,7 +45,7 @@ public class MasterService {
CommonUtil.printLogToConsole("Enabled local images mode ...");
CommonUtil.printLogToConsole("Find images directory ...");
File imagesDir = new File(CommonUtil.rootPath + "/" + DefaultConfig.imagePath);
final File imagesDir = new File(CommonUtil.rootPath + "/" + DefaultConfig.imagePath);
if (imagesDir.exists()) {
if (imagesDir.isDirectory()) {
@@ -52,7 +53,6 @@ public class MasterService {
CommonUtil.printLogToConsole("Find images files ...");
File[] imageList = imagesDir.listFiles();
imagesDir = null;
if (imageList == null || imageList.length < 1) {
DefaultConfig.imageIndex = 0;
@@ -84,71 +84,57 @@ public class MasterService {
DefaultConfig.imageIndex = 0;
}
StringBuffer htmlStr = new StringBuffer();
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
final StringBuilder imageUrlStr = new StringBuilder();
try {
URL url = new URL("https://wall.alphacoders.com/popular.php?page=" + (DefaultConfig.imageIndex + 1));
inputStream = url.openStream();
inputStreamReader = new InputStreamReader(inputStream, "utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
final URL url = new URL(DefaultConfig.imageUrlArr[new Random().nextInt(DefaultConfig.imageUrlArr.length)]);
String line;
while ((line = bufferedReader.readLine()) != null) {
htmlStr.append(line).append("\n");
}
trustAllHttpsCertificates();
HostnameVerifier hv = (urlHostName, session) -> {
System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
return true;
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
bufferedReader.close();
final HttpURLConnection conn = (HttpURLConnection) (url.openConnection());
final URL imageUrl = conn.getURL();
imageUrlStr.append(imageUrl.getProtocol()).append("://").append(imageUrl.getHost()).append(imageUrl.getPath());
} catch (IOException e) {
DefaultConfig.imageIndex = 0;
throw e;
}finally {
try {
if(bufferedReader!=null){
bufferedReader.close();
bufferedReader=null;
}
if(inputStreamReader!=null){
inputStreamReader.close();
inputStreamReader=null;
}
if(inputStream!=null){
inputStream.close();
inputStream=null;
}
} catch (IOException e) {
throw e;
}
}
if (htmlStr.length() > 0) {
final List<String> imageUrlList = new ArrayList<>();
final String IMGURL_REG = "(https://images(.*)thumbbig-(\\d*).jpg)";
Matcher matcher = Pattern.compile(IMGURL_REG).matcher(htmlStr);
while (matcher.find()){
imageUrlList.add(matcher.group());
}
if (imageUrlList.size() < 1) {
DefaultConfig.imageIndex = 0;
return;
}
String imageUrl = imageUrlList.get(new Random().nextInt(imageUrlList.size()));
imageUrl = imageUrl.replace("/thumbbig-", "/");
BufferedImage targetImage = ImageUtil.getImageToScreen(imageUrl, true);
if (imageUrlStr.length() > 0) {
BufferedImage targetImage = ImageUtil.getImageToScreen(imageUrlStr.toString(), true);
CommonUtil.printLogToConsole("Print images to screen ...");
ePaper.drawImage(targetImage);
DefaultConfig.imageIndex++;
}
}
}
/**
* 跳过SSL证书验证
* @throws Exception
*/
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
private static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
}

View File

@@ -1,9 +1,11 @@
package icu.namophice.inkphotoalbum.config;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONObject;
import icu.namophice.inkphotoalbum.utils.CommonUtil;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
/**
* @author Namophice
@@ -11,6 +13,15 @@ import java.io.*;
*/
public class DefaultConfig {
public static final String[] imageUrlArr = {
"https://api.ixiaowai.cn/api/api.php",
"https://api.ixiaowai.cn/api/api2.php",
"https://api.ixiaowai.cn/mcapi/mcapi.php",
"https://api.ixiaowai.cn/mcapi/mcapi2.php",
"https://api.ixiaowai.cn/gqapi/gqapi.php",
"https://api.ixiaowai.cn/gqapi/gqapi2.php"
};
public static final String configFileName = "conf.json";
public static final String cacheFileName = "cache";
@@ -27,7 +38,7 @@ public class DefaultConfig {
* 是否开启图片填充
*/
private static boolean enable_image_fill = false;
public static final boolean enable_image_fill() {
public static boolean enable_image_fill() {
return enable_image_fill;
}
@@ -46,7 +57,7 @@ public class DefaultConfig {
CommonUtil.printLogToConsole("Init config ...");
File configFile = new File(CommonUtil.rootPath + "/" + configFileName);
if (configFile.exists()) {
if (configFile.exists() && !configFile.isDirectory()) {
BufferedReader bufferedReader = null;
final StringBuilder confJsonStrBuilder = new StringBuilder();
@@ -96,9 +107,8 @@ public class DefaultConfig {
/**
* 从cache文件获取imageIndex
* @throws IOException
*/
public static void initImageIndexWithCache() throws IOException {
public static void initImageIndexWithCache() {
CommonUtil.printLogToConsole("Init image index with cache ...");
DefaultConfig.imageIndex = 0;
@@ -126,7 +136,7 @@ public class DefaultConfig {
}
}
if (lineStr.length() > 0) {
if (lineStr != null && lineStr.length() > 0) {
DefaultConfig.imageIndex = Integer.parseInt(lineStr);
}
}

View File

@@ -22,16 +22,14 @@ public class EPaper {
private EPaper() {}
private static EPaper ePaper;
public static EPaper getInstance() {
return ePaper = new EPaper();
return new EPaper();
}
private static GpioPinDigitalOutput CS;
private static GpioPinDigitalOutput DC;
private static GpioPinDigitalOutput RST;
private static GpioPinDigitalInput BUSY;
private static final GpioPinDigitalOutput CS;
private static final GpioPinDigitalOutput DC;
private static final GpioPinDigitalOutput RST;
private static final GpioPinDigitalInput BUSY;
public static SpiDevice spiDevice;
@@ -271,7 +269,7 @@ public class EPaper {
temp |= 0;
}else if (grayNum < GRAY_SCALE_OF_4[2]) {
temp |= 1;
} else if(grayNum <= GRAY_SCALE_OF_4[3]){
} else {
temp |= 0;
}