From 582534512fabc6bd37b86aadad410341325287df Mon Sep 17 00:00:00 2001 From: Namophice Date: Mon, 16 Aug 2021 19:04:45 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/icu/namophice/inkphotoalbum/config/DefaultConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java b/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java index de895bd..3deb7bb 100644 --- a/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java +++ b/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java @@ -46,7 +46,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(); From 3798326643b14d19726a2a5cb4d30b6f805a132c Mon Sep 17 00:00:00 2001 From: Namophice Date: Thu, 9 Feb 2023 13:29:20 +0800 Subject: [PATCH 2/5] =?UTF-8?q?Gradle=E8=BD=ACMaven?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++- build.gradle | 33 ------------------- pom.xml | 30 +++++++++++++++++ settings.gradle | 2 -- .../inkphotoalbum/config/DefaultConfig.java | 2 +- 5 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 build.gradle create mode 100644 pom.xml delete mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore index 840577e..9698d9b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,6 @@ gradle-app.setting .idea/ cache error.log -gradle/ \ No newline at end of file +gradle/ +target +logs diff --git a/build.gradle b/build.gradle deleted file mode 100644 index fbc64b9..0000000 --- a/build.gradle +++ /dev/null @@ -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' - ) - } -} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b8c84a2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + icu.namophice + ink-photo-album + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.23 + + + com.pi4j + pi4j-core + 1.4 + + + + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index 08ebb9d..0000000 --- a/settings.gradle +++ /dev/null @@ -1,2 +0,0 @@ -rootProject.name = 'ink-photo-album' - diff --git a/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java b/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java index 3deb7bb..8bc0fde 100644 --- a/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java +++ b/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java @@ -1,6 +1,6 @@ package icu.namophice.inkphotoalbum.config; -import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson2.JSONObject; import icu.namophice.inkphotoalbum.utils.CommonUtil; import java.io.*; From fa5aeb28ecd4784888ae3e8d13cc15e9da71eb31 Mon Sep 17 00:00:00 2001 From: Namophice Date: Mon, 13 Mar 2023 17:20:05 +0800 Subject: [PATCH 3/5] =?UTF-8?q?1=E3=80=81JDK=E5=8D=87=E8=87=B317=EF=BC=8C2?= =?UTF-8?q?=E3=80=81=E6=9B=B4=E6=96=B0=E5=9B=BE=E7=89=87=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=BA=90=EF=BC=8C3=E3=80=81=E5=BF=BD=E7=95=A5SSL=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 31 ++++- .../icu/namophice/inkphotoalbum/Main.java | 3 - .../inkphotoalbum/business/MasterService.java | 114 ++++++++---------- .../inkphotoalbum/config/DefaultConfig.java | 20 ++- .../inkphotoalbum/driver/EPaper.java | 14 +-- 5 files changed, 101 insertions(+), 81 deletions(-) diff --git a/pom.xml b/pom.xml index b8c84a2..b7a072e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ icu.namophice ink-photo-album - 1.0-SNAPSHOT + 1.1-RELEASE 17 @@ -27,4 +27,33 @@ + + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + + + + icu.namophice.inkphotoalbum.Main + + + + + jar-with-dependencies + + + + + + + + \ No newline at end of file diff --git a/src/main/java/icu/namophice/inkphotoalbum/Main.java b/src/main/java/icu/namophice/inkphotoalbum/Main.java index 6bfd29e..017418e 100644 --- a/src/main/java/icu/namophice/inkphotoalbum/Main.java +++ b/src/main/java/icu/namophice/inkphotoalbum/Main.java @@ -17,9 +17,6 @@ public class Main { // 初始化配置 DefaultConfig.initConfig(); - // 从cache文件获取imageIndex - DefaultConfig.initImageIndexWithCache(); - // 进入主流程 MasterService.master(); } catch (Exception e) { diff --git a/src/main/java/icu/namophice/inkphotoalbum/business/MasterService.java b/src/main/java/icu/namophice/inkphotoalbum/business/MasterService.java index 6ff9f2d..f07c287 100644 --- a/src/main/java/icu/namophice/inkphotoalbum/business/MasterService.java +++ b/src/main/java/icu/namophice/inkphotoalbum/business/MasterService.java @@ -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 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; + } + } + } diff --git a/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java b/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java index 8bc0fde..242d008 100644 --- a/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java +++ b/src/main/java/icu/namophice/inkphotoalbum/config/DefaultConfig.java @@ -3,7 +3,9 @@ package icu.namophice.inkphotoalbum.config; 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; } @@ -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); } } diff --git a/src/main/java/icu/namophice/inkphotoalbum/driver/EPaper.java b/src/main/java/icu/namophice/inkphotoalbum/driver/EPaper.java index f6fca82..02f0d58 100644 --- a/src/main/java/icu/namophice/inkphotoalbum/driver/EPaper.java +++ b/src/main/java/icu/namophice/inkphotoalbum/driver/EPaper.java @@ -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; } From d0e1dc41a9954bbdbb3f4ec71b2740d06bb6b0ca Mon Sep 17 00:00:00 2001 From: Namophice Date: Mon, 13 Mar 2023 18:30:54 +0800 Subject: [PATCH 4/5] =?UTF-8?q?README=E6=96=87=E6=A1=A3=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9c156d5..5481a1a 100644 --- a/README.md +++ b/README.md @@ -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: From 16c218d528afbeaad42a257071d7ade126708a64 Mon Sep 17 00:00:00 2001 From: Namophice Date: Mon, 13 Mar 2023 18:48:44 +0800 Subject: [PATCH 5/5] =?UTF-8?q?pom=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b7a072e..91f360a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ icu.namophice ink-photo-album - 1.1-RELEASE + 1.1-SNAPSHOT 17 @@ -32,6 +32,7 @@ org.apache.maven.plugins maven-assembly-plugin + 3.5.0 package