├── .gitignore ├── .idea ├── compiler.xml ├── encodings.xml ├── misc.xml └── vcs.xml ├── LICENSE ├── README.md ├── pom.xml ├── src └── main │ ├── java │ └── tony │ │ ├── AboutFrame.form │ │ ├── AboutFrame.java │ │ ├── HiJFrame.form │ │ ├── HiJFrame.java │ │ ├── ImageHide.java │ │ ├── ImageTran.java │ │ ├── ImageUtil.java │ │ ├── ImgCanChoose.java │ │ ├── StringBinUtil.java │ │ └── TxtCanChoose.java │ └── resources │ └── icon.png └── static ├── hide.png ├── yaofan.png └── yaofan_hide.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | .idea 25 | 26 | /target/ -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 图片隐写,将要隐藏的文字写入图片中,理论上最大可支持几十M文本。 2 | 3 | 原理比较简单,将输入的文本转为二进制字符串,然后和图片的RGB像素奇偶对应。 4 | 比如: 5 | 图片前三个像素RGB的值为(222,111,23),(222,10,23)(1,58,40),规则为1对奇0对偶,则这三个像素对应存储的二进制为 6 | 011001100。 7 | 8 | 现在已知一个二进制串,只需要对图片像素进行微调,即可记录下这串二进制字符串,这个像素微调对人眼来说是无法区分的。 9 | 提取文本则是进行一个反向运算。 10 | 11 | 更多细节可参阅源码。 12 | 13 | 经过处理的图片和原图在肉眼上几乎没有区别,并且在体积上不会造成明显膨胀。 14 | (处理后的体积可能略微膨胀,甚至会变小,取决于原图色彩的复杂度) 15 | 16 | 注意:本源码并非标准的LSB隐写,使用本软件隐写的内容使用Stegsolve类软件不能简单提取。 17 | 18 | GUI运行方式: 19 | ``` 20 | java -jar -Dfile.encoding=UTF-8 hide.jar 21 | ``` 22 | 命令行运行方式: 23 | ``` 24 | java -cp target/hide.jar -Dfile.encoding=UTF-8 tony.ImageHide 25 | ``` 26 | APP截图: 27 | 28 | ![APP截图][3] 29 | 30 | 测试原图: 31 | 32 | ![原图][1] 33 | 34 | 测试处理后图片: 35 | 36 | ![处理后图片][2] 37 | 38 | 隐藏文字:组织上已经决定了,今天的垃圾都归你 39 | 40 | 另:增加了图片简单劣化的功能。 41 | 运行方式: 42 | ``` 43 | java -cp target/hide.jar -Dfile.encoding=UTF-8 tony.ImageTran 44 | ``` 45 | Todo: 46 | - [x] 修复提取内容上最后一个文字乱码的bug 47 | - [x] 待隐写的文本内容有硬回车导致回车前内容被截取的bug 48 | - [ ] 随机位置隐写的功能 49 | 50 | [1]: ./static/yaofan.png 51 | [2]: ./static/yaofan_hide.png 52 | [3]: ./static/hide.png 53 | 54 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | tony 8 | hideImage 9 | 1.0 10 | hideImage 11 | https://github.com/iminto/hideImage 12 | 13 | 14 | UTF-8 15 | UTF-8 16 | UTF-8 17 | 1.8 18 | 1.8 19 | 20 | 21 | 22 | 23 | 24 | 25 | hide 26 | 27 | 28 | src/main/resources 29 | 30 | **/*.xml 31 | **/*.properties 32 | **/*.png 33 | 34 | 35 | 36 | src/main/java 37 | 38 | **/*.xml 39 | **/*.properties 40 | 41 | 42 | 43 | 44 | 45 | 46 | maven-jar-plugin 47 | 3.0.2 48 | 49 | 50 | 51 | tony.HiJFrame 52 | true 53 | lib/ 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.apache.maven.plugins 62 | maven-compiler-plugin 63 | 3.1 64 | 65 | 1.8 66 | 1.8 67 | UTF-8 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /src/main/java/tony/AboutFrame.form: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /src/main/java/tony/AboutFrame.java: -------------------------------------------------------------------------------- 1 | package tony; 2 | 3 | import javax.swing.ImageIcon; 4 | import javax.swing.JFrame; 5 | import java.net.URL; 6 | 7 | public class AboutFrame extends javax.swing.JFrame { 8 | public AboutFrame() { 9 | initComponents(); 10 | setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 11 | setLocationRelativeTo(null); 12 | setTitle("关于"); 13 | URL imUrl = getClass().getResource("/icon.png"); 14 | ImageIcon icon = new ImageIcon(imUrl); 15 | setIconImage(icon.getImage()); 16 | setResizable(false); 17 | } 18 | @SuppressWarnings("unchecked") 19 | // //GEN-BEGIN:initComponents 20 | private void initComponents() { 21 | 22 | jLabel1 = new javax.swing.JLabel(); 23 | 24 | setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 25 | 26 | jLabel1.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N 27 | jLabel1.setText("本APP可实现图片隐写功能,将文本写入PNG图片中且不影响图片正常使用,并且可以随时提取。 \n
作者:waitfox@qq.com\n
https://github.com/iminto/hideImage\n
如果中文编码出错,请使用java -jar -Dfile.encoding=UTF-8 hide.jar形式运行"); 28 | jLabel1.setToolTipText(""); 29 | 30 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 31 | getContentPane().setLayout(layout); 32 | layout.setHorizontalGroup( 33 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 34 | .addGroup(layout.createSequentialGroup() 35 | .addContainerGap() 36 | .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE) 37 | .addContainerGap()) 38 | ); 39 | layout.setVerticalGroup( 40 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 41 | .addGroup(layout.createSequentialGroup() 42 | .addGap(22, 22, 22) 43 | .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) 44 | .addContainerGap(23, Short.MAX_VALUE)) 45 | ); 46 | 47 | pack(); 48 | }//
//GEN-END:initComponents 49 | 50 | 51 | public static void main(String args[]) { 52 | java.awt.EventQueue.invokeLater(new Runnable() { 53 | public void run() { 54 | AboutFrame aboutFrame=new AboutFrame(); 55 | aboutFrame.setVisible(true); 56 | aboutFrame.setTitle("关于"); 57 | aboutFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 58 | } 59 | }); 60 | } 61 | 62 | // Variables declaration - do not modify//GEN-BEGIN:variables 63 | private javax.swing.JLabel jLabel1; 64 | // End of variables declaration//GEN-END:variables 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/tony/HiJFrame.form: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /src/main/java/tony/HiJFrame.java: -------------------------------------------------------------------------------- 1 | package tony; 2 | 3 | import javax.swing.*; 4 | import java.awt.*; 5 | import java.io.File; 6 | import java.io.IOException; 7 | import java.net.URL; 8 | import java.nio.file.Files; 9 | import java.nio.file.Paths; 10 | import java.util.logging.Level; 11 | import java.util.logging.Logger; 12 | 13 | public class HiJFrame extends javax.swing.JFrame { 14 | 15 | private String imagePath; 16 | private ImageHide rc; 17 | private ImageIcon icon; 18 | 19 | public HiJFrame() { 20 | initComponents(); 21 | rc = new ImageHide(); 22 | URL imUrl = getClass().getResource("/icon.png"); 23 | icon = new ImageIcon(imUrl); 24 | setIconImage(icon.getImage()); 25 | setResizable(false); 26 | } 27 | 28 | @SuppressWarnings("unchecked") 29 | // //GEN-BEGIN:initComponents 30 | private void initComponents() { 31 | 32 | jScrollPane2 = new javax.swing.JScrollPane(); 33 | hideText = new javax.swing.JTextPane(); 34 | hideBtn = new javax.swing.JButton(); 35 | recBtn = new javax.swing.JButton(); 36 | imgShowLabel = new javax.swing.JLabel(); 37 | statusBar = new javax.swing.JPanel(); 38 | statusLabel = new javax.swing.JLabel(); 39 | fileMenuBar = new javax.swing.JMenuBar(); 40 | fileMenu = new javax.swing.JMenu(); 41 | imgOpen = new javax.swing.JMenuItem(); 42 | txtOpen = new javax.swing.JMenuItem(); 43 | appExit = new javax.swing.JMenuItem(); 44 | helpMenu = new javax.swing.JMenu(); 45 | aboutMenuItem = new javax.swing.JMenuItem(); 46 | 47 | setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 48 | setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 49 | 50 | hideText.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N 51 | hideText.setText("输入要隐写的内容或从文件菜单中加载本地文本文件"); 52 | jScrollPane2.setViewportView(hideText); 53 | 54 | hideBtn.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N 55 | hideBtn.setForeground(new java.awt.Color(0, 204, 204)); 56 | hideBtn.setText("隐写"); 57 | hideBtn.addActionListener(new java.awt.event.ActionListener() { 58 | public void actionPerformed(java.awt.event.ActionEvent evt) { 59 | hideBtnActionPerformed(evt); 60 | } 61 | }); 62 | 63 | recBtn.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N 64 | recBtn.setText("提取"); 65 | recBtn.addActionListener(new java.awt.event.ActionListener() { 66 | public void actionPerformed(java.awt.event.ActionEvent evt) { 67 | recBtnActionPerformed(evt); 68 | } 69 | }); 70 | 71 | statusBar.setToolTipText(""); 72 | statusLabel.setToolTipText(""); 73 | 74 | javax.swing.GroupLayout statusBarLayout = new javax.swing.GroupLayout(statusBar); 75 | statusBar.setLayout(statusBarLayout); 76 | statusBarLayout.setHorizontalGroup( 77 | statusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 78 | .addComponent(statusLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 79 | ); 80 | statusBarLayout.setVerticalGroup( 81 | statusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 82 | .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE) 83 | ); 84 | 85 | fileMenuBar.setName("fileMenuBar"); // NOI18N 86 | 87 | fileMenu.setText("文件"); 88 | 89 | imgOpen.setText("选择图片"); 90 | imgOpen.addActionListener(new java.awt.event.ActionListener() { 91 | public void actionPerformed(java.awt.event.ActionEvent evt) { 92 | imgOpenActionPerformed(evt); 93 | } 94 | }); 95 | fileMenu.add(imgOpen); 96 | 97 | txtOpen.setText("选择文本文件"); 98 | txtOpen.addActionListener(new java.awt.event.ActionListener() { 99 | public void actionPerformed(java.awt.event.ActionEvent evt) { 100 | txtOpenActionPerformed(evt); 101 | } 102 | }); 103 | fileMenu.add(txtOpen); 104 | 105 | appExit.setText("退出"); 106 | appExit.addActionListener(new java.awt.event.ActionListener() { 107 | public void actionPerformed(java.awt.event.ActionEvent evt) { 108 | appExitActionPerformed(evt); 109 | } 110 | }); 111 | fileMenu.add(appExit); 112 | 113 | fileMenuBar.add(fileMenu); 114 | 115 | helpMenu.setText("帮助"); 116 | 117 | aboutMenuItem.setText("关于"); 118 | aboutMenuItem.addActionListener(new java.awt.event.ActionListener() { 119 | public void actionPerformed(java.awt.event.ActionEvent evt) { 120 | aboutMenuItemActionPerformed(evt); 121 | } 122 | }); 123 | helpMenu.add(aboutMenuItem); 124 | 125 | fileMenuBar.add(helpMenu); 126 | 127 | setJMenuBar(fileMenuBar); 128 | 129 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 130 | getContentPane().setLayout(layout); 131 | layout.setHorizontalGroup( 132 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 133 | .addGroup(layout.createSequentialGroup() 134 | .addGap(19, 19, 19) 135 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 136 | .addComponent(imgShowLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 137 | .addGroup(layout.createSequentialGroup() 138 | .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE) 139 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE) 140 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 141 | .addComponent(hideBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) 142 | .addComponent(recBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)))) 143 | .addGap(30, 30, 30)) 144 | .addGroup(layout.createSequentialGroup() 145 | .addContainerGap() 146 | .addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 147 | ); 148 | layout.setVerticalGroup( 149 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 150 | .addGroup(layout.createSequentialGroup() 151 | .addContainerGap() 152 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 153 | .addGroup(layout.createSequentialGroup() 154 | .addComponent(hideBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) 155 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 156 | .addComponent(recBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)) 157 | .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)) 158 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 159 | .addComponent(imgShowLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE) 160 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 161 | .addComponent(statusBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 162 | ); 163 | 164 | pack(); 165 | }// //GEN-END:initComponents 166 | 167 | private void txtOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtOpenActionPerformed 168 | JFileChooser txtChooser = new JFileChooser(){ 169 | @Override 170 | protected JDialog createDialog(Component parent ) throws HeadlessException { 171 | JDialog dialog = super.createDialog( parent ); 172 | dialog.setIconImage(icon.getImage()); 173 | return dialog; 174 | } 175 | };; 176 | txtChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 177 | txtChooser.setFileFilter(new TxtCanChoose()); 178 | int returnVal = txtChooser.showDialog(new JLabel(), "选择"); 179 | if (returnVal == txtChooser.APPROVE_OPTION) { 180 | File file = txtChooser.getSelectedFile(); 181 | String content = readFile(file.getAbsolutePath()); 182 | hideText.setText(content); 183 | } 184 | }//GEN-LAST:event_txtOpenActionPerformed 185 | 186 | private void imgOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imgOpenActionPerformed 187 | JFileChooser pngChooser = new JFileChooser(){ 188 | @Override 189 | protected JDialog createDialog(Component parent ) throws HeadlessException { 190 | JDialog dialog = super.createDialog(parent); 191 | dialog.setIconImage( icon.getImage() ); 192 | return dialog; 193 | } 194 | }; 195 | pngChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 196 | pngChooser.setFileFilter(new ImgCanChoose()); 197 | int returnVal = pngChooser.showDialog(new JLabel(), "选择"); 198 | if (returnVal == pngChooser.APPROVE_OPTION) { 199 | File file = pngChooser.getSelectedFile(); 200 | imagePath = file.getPath(); 201 | String fileName=file.getName(); 202 | try { 203 | rc.addPng(imagePath); 204 | imgShowLabel.setHorizontalAlignment(JLabel.CENTER); 205 | ImageIcon image = new ImageIcon(file.getPath()); 206 | //自适应缩放 207 | if(rc.getWidth()>550||rc.getHeight()>200) { 208 | int[] size=ImageUtil.getAutoSize(rc.width,rc.height,550,200); 209 | Image img = image.getImage(); 210 | img = img.getScaledInstance(size[0], size[1], Image.SCALE_DEFAULT); 211 | image.setImage(img); 212 | } 213 | imgShowLabel.setIcon(image); 214 | 215 | long size = file.length(); 216 | int enable=rc.getEnabled(); 217 | String imgSize=ImageUtil.formatFileSize(size); 218 | statusLabel.setText("图片:"+fileName+" 大小:" + imgSize+",可隐写字节数:"+ImageUtil.formatFileSize(enable)); 219 | } catch (Exception ex) { 220 | JOptionPane.showMessageDialog(this, ex.getMessage(), "错误 ", 0); 221 | } 222 | } 223 | }//GEN-LAST:event_imgOpenActionPerformed 224 | 225 | private void appExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_appExitActionPerformed 226 | System.exit(0); 227 | }//GEN-LAST:event_appExitActionPerformed 228 | 229 | private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed 230 | new AboutFrame().setVisible(true); 231 | }//GEN-LAST:event_aboutMenuItemActionPerformed 232 | 233 | private void hideBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hideBtnActionPerformed 234 | if (!ImageUtil.isNotEmpty(hideText.getText()) || !ImageUtil.isNotEmpty(imagePath)) { 235 | JOptionPane.showMessageDialog(this, "图片和文字不可为空!", "错误 ", 0); 236 | } else { 237 | try { 238 | rc.addText(hideText.getText()); 239 | rc.Hide(); 240 | JOptionPane.showMessageDialog(this, "隐写图片已生成至原目录中!", "成功 ", 1); 241 | Logger.getLogger(HiJFrame.class.getName()).log(Level.INFO, "图片->"+rc.getFileLocation()+"隐写成功"); 242 | } catch (Exception ex) { 243 | JOptionPane.showMessageDialog(this, ex.getMessage(), "错误 ", 0); 244 | ex.printStackTrace(); 245 | } 246 | } 247 | }//GEN-LAST:event_hideBtnActionPerformed 248 | 249 | private void recBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recBtnActionPerformed 250 | if (!ImageUtil.isNotEmpty(imagePath)) { 251 | JOptionPane.showMessageDialog(this, "要提取的图片不可为空!", "错误 ", 0); 252 | } else { 253 | try { 254 | String extractStr = rc.ImageExtract(imagePath); 255 | hideText.setText(extractStr); 256 | JOptionPane.showMessageDialog(this, "隐写在图片中的文字已提取到文本框中!", "成功 ", 1); 257 | } catch (Exception e) { 258 | JOptionPane.showMessageDialog(this, "隐写文字提取失败!\r\n可能你輸入的图片并非本程序生成", "错误 ", 0); 259 | Logger.getLogger(HiJFrame.class.getName()).log(Level.SEVERE, "图片->"+rc.getFileLocation()+"提取失败\r\n"+e); 260 | e.printStackTrace(); 261 | } 262 | } 263 | }//GEN-LAST:event_recBtnActionPerformed 264 | 265 | public static void main(String args[]) { 266 | java.awt.EventQueue.invokeLater(new Runnable() { 267 | public void run() { 268 | HiJFrame jframe = new HiJFrame(); 269 | jframe.setVisible(true); 270 | jframe.setTitle("图片隐写助手"); 271 | jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 272 | jframe.setLocationRelativeTo(null); 273 | } 274 | }); 275 | } 276 | 277 | public static String readFile(String filePath) { 278 | try { 279 | String content = new String(Files.readAllBytes(Paths.get(filePath)), "UTF-8"); 280 | return content; 281 | } catch (IOException ex) { 282 | Logger.getLogger(HiJFrame.class.getName()).log(Level.WARNING, "读取文本文件内容出错", ex); 283 | return ""; 284 | } 285 | } 286 | 287 | // Variables declaration - do not modify//GEN-BEGIN:variables 288 | private javax.swing.JMenuItem aboutMenuItem; 289 | private javax.swing.JMenuItem appExit; 290 | private javax.swing.JMenu fileMenu; 291 | private javax.swing.JMenuBar fileMenuBar; 292 | private javax.swing.JMenu helpMenu; 293 | private javax.swing.JButton hideBtn; 294 | private javax.swing.JTextPane hideText; 295 | private javax.swing.JMenuItem imgOpen; 296 | private javax.swing.JLabel imgShowLabel; 297 | private javax.swing.JScrollPane jScrollPane2; 298 | private javax.swing.JButton recBtn; 299 | private javax.swing.JPanel statusBar; 300 | private javax.swing.JLabel statusLabel; 301 | private javax.swing.JMenuItem txtOpen; 302 | // End of variables declaration//GEN-END:variables 303 | } 304 | -------------------------------------------------------------------------------- /src/main/java/tony/ImageHide.java: -------------------------------------------------------------------------------- 1 | package tony; 2 | 3 | import javax.imageio.ImageIO; 4 | import java.awt.image.BufferedImage; 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | /** 11 | * @Author 白菜不是菜 12 | * @Version V1.0 * 13 | * @Description * 14 | * @Date 19-3-20 下午2:33 15 | **/ 16 | public class ImageHide { 17 | 18 | public int enabled=0;//可处理的最大字节数 19 | 20 | public String fileLocation="";//图片地址 21 | 22 | private String hideStr;//要隐写的文字 23 | 24 | private String binStr; 25 | 26 | public int height; 27 | 28 | public int width; 29 | 30 | private String newFileName=""; 31 | 32 | private String findStr=""; 33 | 34 | public BufferedImage addPng(String fileLocation){ 35 | if(fileLocation==null ||fileLocation.equals("")){ 36 | throw new RuntimeException("文件名不能为空"); 37 | } 38 | this.fileLocation=fileLocation; 39 | this.fileLocation=this.fileLocation.replaceAll("\\\\","/"); 40 | BufferedImage bufferedImage = null; 41 | if(!ImageUtil.validPng(fileLocation)){ 42 | //JPEG是有损格式,保存JPEG并重新加载后颜色将不会完全相同,GIF比较复杂暂不考虑 43 | throw new RuntimeException("目前仅支持PNG格式"); 44 | } 45 | try { 46 | File file = new File(fileLocation); 47 | bufferedImage = ImageIO.read(file); 48 | this.height = bufferedImage.getHeight(); 49 | this.width = bufferedImage.getWidth(); 50 | if(this.height<5||this.width<5){ 51 | throw new RuntimeException("哥,图片像素也太小了吧"); 52 | } 53 | int pixSum=this.width*this.height; 54 | int enabled=((pixSum-5)*3)/8; 55 | this.enabled=enabled; 56 | } catch (IOException e) { 57 | e.printStackTrace(); 58 | } 59 | return bufferedImage; 60 | } 61 | 62 | protected String addText(String hideStr){ 63 | if(hideStr==null ||hideStr.equals("")){ 64 | throw new RuntimeException("要隐藏的文字不能为空"); 65 | } 66 | this.hideStr=hideStr; 67 | System.out.println("要隐藏的内容长度(一个中文三个字节):"+hideStr.length()); 68 | byte[] bytes = StringBinUtil.strToByteArray(hideStr); 69 | String binStr = StringBinUtil.byteArrayToString(bytes); 70 | this.binStr=binStr; 71 | int binStrLength=binStr.length(); 72 | System.out.println("要隐藏的bin长度:"+binStrLength); 73 | if(binStrLength>=this.enabled*8){ 74 | //如果输入的字数太多,不报错,但是提示会丢失数据 75 | System.out.println("允许处理的最大字节数是:"+enabled+",你目前的字节数是:"+binStrLength/8+",继续操作会导致丢失一部分数据"); 76 | } 77 | return binStr; 78 | } 79 | 80 | private String addFile(String file){ 81 | try { 82 | String content = new Scanner(new File(file)).useDelimiter("\\Z").next(); 83 | return content; 84 | } catch (FileNotFoundException e) { 85 | e.printStackTrace(); 86 | return null; 87 | } 88 | 89 | } 90 | 91 | public void Hide() { 92 | BufferedImage bufferedImage = addPng(fileLocation); 93 | int count = 0;//像素的位置 94 | int height = this.height; 95 | int width =this.width; 96 | int binStrLength=binStr.length(); 97 | String lengthStr=StringBinUtil.intToBinStr(binStrLength);//长度的字符串 98 | System.out.println("长度转成二进制:"+lengthStr); 99 | int offsetx=0; 100 | int rewrite=0; 101 | for (int j1 = bufferedImage.getMinY(); j1 < height; j1++) { 102 | for (int j2 = bufferedImage.getMinX(); j2 < width; j2++) { 103 | count = (j1 * width + j2) * 3; 104 | int rgb = bufferedImage.getRGB(j2, j1);//这里得到的是负数,Color mycolor = new Color(img.getRGB(x, y));int red = mycolor.getRed();这样可以得到正数,但是创建了太多对象效率比较差 105 | int color = rgb; 106 | int red = color & 0xff; 107 | int green = (color & 0xff00) >> 8; 108 | int blue = (color & 0xff0000) >> 16;; 109 | int alpha = (color & 0xff000000) >>> 24; 110 | int newColor = rgb; 111 | if (count < binStr.length() - 2) { 112 | int offset = count; 113 | if (binStr.charAt(offset) == '1') { 114 | if (red % 2 == 0) { 115 | red += 1;//如果二进制是1,偶数变奇数,保证1对奇0对偶 116 | } 117 | } else {//如果二进制是0,奇数变偶数 118 | if (red % 2 == 1) { 119 | red -= 1; 120 | } 121 | } 122 | offset += 1; 123 | if (binStr.charAt(offset) == '1') { 124 | if (green % 2 == 0) { 125 | green += 1; 126 | } 127 | } else { 128 | if (green % 2 == 1) { 129 | green -= 1; 130 | } 131 | } 132 | offset += 1; 133 | if (binStr.charAt(offset) == '1') { 134 | if (blue % 2 == 0) { 135 | blue += 1; 136 | } 137 | } else { 138 | if (blue % 2 == 1) { 139 | blue -= 1; 140 | } 141 | } 142 | newColor = ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0); 143 | rewrite+=1; 144 | // System.out.println(String.format("此处坐标(x=%d,y=%d),写入新的RGB:%d,处理次数:%d",j2,j1,newColor,rewrite)); 145 | } 146 | 147 | if((j1==height-1)&&(j2+5>=width)){//to note the length 148 | if (lengthStr.charAt(offsetx) == '1') { 149 | if (red % 2 == 0) { 150 | red += 1;//如果二进制是1,偶数变奇数,保证1对奇0对偶 151 | } 152 | } else {//如果二进制是0,奇数变偶数 153 | if (red % 2 == 1) { 154 | red -= 1; 155 | } 156 | } 157 | offsetx += 1; 158 | if (lengthStr.charAt(offsetx) == '1') { 159 | if (green % 2 == 0) { 160 | green += 1; 161 | } 162 | } else { 163 | if (green % 2 == 1) { 164 | green -= 1; 165 | } 166 | } 167 | offsetx += 1; 168 | if (lengthStr.charAt(offsetx) == '1') { 169 | if (blue % 2 == 0) { 170 | blue += 1; 171 | } 172 | } else { 173 | if (blue % 2 == 1) { 174 | blue -= 1; 175 | } 176 | } 177 | offsetx += 1; 178 | newColor = ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0); 179 | rewrite+=1; 180 | // System.out.println(String.format("此处坐标(x=%d,y=%d),为了计入长度写入新的RGB:%d,处理次数:%d",j2,j1,newColor,rewrite)); 181 | } 182 | bufferedImage.setRGB(j2, j1, newColor); 183 | } 184 | } 185 | try { 186 | String newFileName=this.fileLocation.substring(0,fileLocation.lastIndexOf("/")); 187 | newFileName=newFileName+this.fileLocation.substring(fileLocation.lastIndexOf("/"),fileLocation.lastIndexOf(".")); 188 | this.newFileName=newFileName+"_hide.png"; 189 | ImageIO.write(bufferedImage, "png", new File(this.newFileName)); 190 | } catch (IOException e) { 191 | e.printStackTrace(); 192 | } 193 | } 194 | 195 | 196 | public String ImageExtract(String fileLocation) { 197 | if(fileLocation==null ||fileLocation.equals("")){ 198 | throw new RuntimeException("要提取文字的文件名不能为空"); 199 | } 200 | if(!ImageUtil.validPng(fileLocation)){ 201 | throw new RuntimeException("目前仅支持PNG格式"); 202 | } 203 | StringBuilder binStr = new StringBuilder(); 204 | StringBuilder lenBinStr = new StringBuilder(15); 205 | int endLen=0; 206 | try { 207 | File file = new File(fileLocation); 208 | BufferedImage bufferedImage = ImageIO.read(file); 209 | int count = 0;//像素的位置 210 | int height = bufferedImage.getHeight(); 211 | int width = bufferedImage.getWidth(); 212 | for(int y=height-1;y> 16) & 0xFF; 217 | int green = (value >> 8) & 0xFF; 218 | int blue = (value >> 0) & 0xFF; 219 | if (red % 2 == 0) { 220 | lenBinStr.append(0); 221 | } else { 222 | lenBinStr.append(1); 223 | } 224 | if (green % 2 == 0) { 225 | lenBinStr.append(0); 226 | } else { 227 | lenBinStr.append(1); 228 | } 229 | if (blue % 2 == 0) { 230 | lenBinStr.append(0); 231 | } else { 232 | lenBinStr.append(1); 233 | } 234 | } 235 | } 236 | endLen=Integer.parseInt(lenBinStr.toString(),2); 237 | System.out.println("提取到的长度:"+endLen); 238 | lableY: 239 | for (int j1 = bufferedImage.getMinY(); j1 < height; j1++) { 240 | lableX: 241 | for (int j2 = bufferedImage.getMinX(); j2 < width; j2++) { 242 | count = (j1 * width + j2) * 3; 243 | int rgb = bufferedImage.getRGB(j2, j1); 244 | int value = 0xff000000 | rgb; 245 | int red = (value >> 16) & 0xFF; 246 | int green = (value >> 8) & 0xFF; 247 | int blue = (value >> 0) & 0xFF; 248 | if (count>=endLen) { 249 | break lableY; 250 | } 251 | if (red % 2 == 0) { 252 | binStr.append(0); 253 | } else { 254 | binStr.append(1); 255 | } 256 | if (green % 2 == 0) { 257 | binStr.append(0); 258 | } else { 259 | binStr.append(1); 260 | } 261 | if (blue % 2 == 0) { 262 | binStr.append(0); 263 | } else { 264 | binStr.append(1); 265 | } 266 | } 267 | } 268 | bufferedImage = null; 269 | } catch (Exception e) { 270 | e.printStackTrace(); 271 | } 272 | byte[] old = StringBinUtil.binStrToByteArray(binStr.substring(0,endLen)); 273 | this.findStr=new String(old); 274 | return this.findStr; 275 | } 276 | 277 | public static void main(String[] args) { 278 | Scanner in = new Scanner(System.in); 279 | System.out.println("请选择操作:1.隐藏文字 2.隱藏文本 3.提取文字"); 280 | String option=in.nextLine(); 281 | if(option==null||(!option.equals("1") && !option.equals("2") && !option.equals("3"))){ 282 | System.out.println("输入错误 请输入数字1-3"); 283 | System.exit(-1); 284 | } 285 | if(option.equals("1")) { 286 | System.out.println("输入png图片地址(绝对路径,统一使用 / 格式)"); 287 | String png = in.nextLine(); 288 | ImageHide rc = new ImageHide(); 289 | rc.addPng(png); 290 | System.out.println("输入要隐藏的文本文件,当前图片最大支持" + rc.getEnabled() + "字节"); 291 | rc.addText(in.nextLine()); 292 | rc.Hide(); 293 | System.out.println("隐写文件已生成,文件地址:"+rc.getNewFileName()); 294 | }else if(option.equals("2")){ 295 | System.out.println("输入png图片地址(绝对路径,统一使用 / 格式)"); 296 | String png = in.nextLine(); 297 | ImageHide rc = new ImageHide(); 298 | rc.addPng(png); 299 | System.out.println("输入要隐藏的文本文件路径(绝对路径,统一使用 / 格式),当前图片最大支持" + rc.getEnabled() + "字节"); 300 | String addText=rc.addFile(in.nextLine()); 301 | rc.addText(addText); 302 | rc.Hide(); 303 | System.out.println("隐写文件已生成,文件地址:"+rc.getNewFileName()); 304 | }else{ 305 | System.out.println("输入要提取文字的png图片地址(绝对路径,统一使用 / 格式)"); 306 | String png = in.nextLine(); 307 | ImageHide rc = new ImageHide(); 308 | String extractStr = rc.ImageExtract(png); 309 | System.out.println("提取出文字:\n"+extractStr); 310 | } 311 | 312 | } 313 | 314 | public int getEnabled() { 315 | return enabled; 316 | } 317 | 318 | public void setEnabled(int enabled) { 319 | this.enabled = enabled; 320 | } 321 | 322 | public String getFileLocation() { 323 | return fileLocation; 324 | } 325 | 326 | public void setFileLocation(String fileLocation) { 327 | this.fileLocation = fileLocation; 328 | } 329 | 330 | public String getHideStr() { 331 | return hideStr; 332 | } 333 | 334 | public void setHideStr(String hideStr) { 335 | this.hideStr = hideStr; 336 | } 337 | 338 | public String getBinStr() { 339 | return binStr; 340 | } 341 | 342 | public void setBinStr(String binStr) { 343 | this.binStr = binStr; 344 | } 345 | 346 | public int getHeight() { 347 | return height; 348 | } 349 | 350 | public void setHeight(int height) { 351 | this.height = height; 352 | } 353 | 354 | public int getWidth() { 355 | return width; 356 | } 357 | 358 | public void setWidth(int width) { 359 | this.width = width; 360 | } 361 | 362 | public String getNewFileName() { 363 | return newFileName; 364 | } 365 | 366 | public void setNewFileName(String newFileName) { 367 | this.newFileName = newFileName; 368 | } 369 | } -------------------------------------------------------------------------------- /src/main/java/tony/ImageTran.java: -------------------------------------------------------------------------------- 1 | package tony; 2 | 3 | import javax.imageio.ImageIO; 4 | import java.awt.image.BufferedImage; 5 | import java.io.File; 6 | import java.io.IOException; 7 | import java.util.Random; 8 | import java.util.Scanner; 9 | 10 | public class ImageTran { 11 | 12 | public void turnColor(String fileLocation,String tranColor,Integer forward) { 13 | BufferedImage bufferedImage = ImageUtil.addImage(fileLocation); 14 | if(bufferedImage==null){ 15 | throw new RuntimeException("不是图片文件"); 16 | } 17 | String suffix=fileLocation.substring(fileLocation.lastIndexOf(".")+1).toLowerCase();; 18 | int height = bufferedImage.getHeight(); 19 | int width = bufferedImage.getWidth(); 20 | Random random=new Random(); 21 | Integer[] randomArray=new Integer[100]; 22 | for (int i=0;i<100;i++) { 23 | randomArray[i]=random.nextInt(255); 24 | } 25 | for (int j1 = bufferedImage.getMinY(); j1 < height; j1++) { 26 | for (int j2 = bufferedImage.getMinX(); j2 < width; j2++) { 27 | int color = bufferedImage.getRGB(j2, j1); 28 | int red = color & 0xff; 29 | int green = (color & 0xff00) >> 8; 30 | int blue = (color & 0xff0000) >> 16;; 31 | int alpha = (color & 0xff000000) >>> 24; 32 | int newColor = color; 33 | if(("red").equalsIgnoreCase(tranColor)){ 34 | red=255;//红色打满 35 | }else if(("blue").equalsIgnoreCase(tranColor)){ 36 | blue=255; 37 | }else if (("green").equalsIgnoreCase(tranColor)){ 38 | green=255; 39 | }else{ 40 | double yui = red * 0.299 + green * 0.587 + blue * 0.114; 41 | if (yui >= 240) { 42 | red=10; 43 | blue=20; 44 | green=30; 45 | }else if (yui >= 210) {//很明亮 46 | red=40; 47 | blue=50; 48 | green=60; 49 | }else if (yui >= 192) { 50 | red=70; 51 | blue=80; 52 | green=90; 53 | }else if (yui >= 152) { 54 | if (j2 % 4 == 0) { 55 | red -= randomArray[j2 % 100]; 56 | } else if (j2 % 4 == 1) { 57 | green -= randomArray[j2 % 100]; 58 | } else if (j2 % 4 == 2) { 59 | blue -= randomArray[j2 % 100]; 60 | } 61 | }else { 62 | if(j2%10==0) { 63 | red += randomArray[j2 % 100]; 64 | green += randomArray[j2 % 100]; 65 | blue += randomArray[j2 % 100]; 66 | alpha += randomArray[j2 % 100]; 67 | } 68 | } 69 | } 70 | newColor = ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0); 71 | bufferedImage.setRGB(j2, j1, newColor); 72 | } 73 | } 74 | try { 75 | String newFileName=fileLocation.substring(0,fileLocation.lastIndexOf("/")); 76 | newFileName=newFileName+fileLocation.substring(fileLocation.lastIndexOf("/"),fileLocation.lastIndexOf(".")); 77 | newFileName=newFileName+"_"+tranColor+"."+suffix; 78 | ImageIO.write(bufferedImage, suffix, new File(newFileName)); 79 | } catch (IOException e) { 80 | e.printStackTrace(); 81 | } 82 | } 83 | 84 | public static void main(String[] args) { 85 | Scanner in = new Scanner(System.in); 86 | System.out.println("请选择操作:1.变色"); 87 | String option=in.nextLine(); 88 | if(option==null||(!option.equals("1") && !option.equals("2") && !option.equals("3"))){ 89 | System.out.println("输入错误 请输入数字1-3"); 90 | System.exit(-1); 91 | } 92 | if(option.equals("1")) { 93 | System.out.println("输入图片地址(绝对路径,统一使用 / 格式,支持jpg/gif/png)"); 94 | String png = in.nextLine(); 95 | System.out.println("输入要劣化的颜色(red,blue,green),输入其它选择则进行随机操作"); 96 | String color = in.nextLine(); 97 | ImageTran tran = new ImageTran(); 98 | tran.turnColor(png,color,1); 99 | System.out.println("变色文件已生成到同目录"); 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/tony/ImageUtil.java: -------------------------------------------------------------------------------- 1 | package tony; 2 | 3 | import javax.imageio.ImageIO; 4 | import java.awt.image.BufferedImage; 5 | import java.io.*; 6 | import java.net.URLConnection; 7 | import java.text.DecimalFormat; 8 | 9 | /** 10 | * @Author 白菜不是菜 chenwen2@wdai.com 11 | * @Version V1.0 12 | * @Description 13 | * @Date 19-3-21 下午12:52 14 | **/ 15 | public class ImageUtil { 16 | 17 | public static boolean validPng(String fileLocation) { 18 | String suffix = fileLocation.substring(fileLocation.lastIndexOf(".") + 1); 19 | if (!("png").equals(suffix.toLowerCase())) { 20 | //JPEG是有损格式,保存JPEG并重新加载后颜色将不会完全相同,GIF比较复杂暂不考虑 21 | return false; 22 | } 23 | File file = new File(fileLocation); 24 | InputStream is = null; 25 | FileInputStream fileInputStream = null; 26 | try { 27 | fileInputStream = new FileInputStream(file); 28 | is = new BufferedInputStream(fileInputStream); 29 | String mimeType = URLConnection.guessContentTypeFromStream(is); 30 | return ("image/png").equals(mimeType); 31 | } catch (IOException e) { 32 | e.printStackTrace(); 33 | return false; 34 | } finally { 35 | try { 36 | fileInputStream.close(); 37 | is.close(); 38 | } catch (IOException e) { 39 | } 40 | } 41 | } 42 | 43 | 44 | public static boolean isWin() { 45 | String osName = System.getProperty("os.name"); 46 | return osName != null && osName.startsWith("Windows"); 47 | } 48 | 49 | public static boolean isNotEmpty(String str) { 50 | return str != null && !("").equals(str); 51 | } 52 | 53 | public static String formatFileSize(long size) { 54 | String hrSize = null; 55 | double b = size; 56 | double k = size / 1024.0; 57 | double m = ((size / 1024.0) / 1024.0); 58 | double g = (((size / 1024.0) / 1024.0) / 1024.0); 59 | double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.0); 60 | 61 | DecimalFormat dec = new DecimalFormat("0.00"); 62 | if (t > 1) { 63 | hrSize = dec.format(t).concat(" TB"); 64 | } else if (g > 1) { 65 | hrSize = dec.format(g).concat(" GB"); 66 | } else if (m > 1) { 67 | hrSize = dec.format(m).concat(" MB"); 68 | } else if (k > 1) { 69 | hrSize = dec.format(k).concat(" KB"); 70 | } else { 71 | hrSize = dec.format(b).concat(" Bytes"); 72 | } 73 | return hrSize; 74 | } 75 | 76 | public static int[] getAutoSize(int width, int height, int maxWidth, int maxHeight) { 77 | if (width <= 0 || height <= 0 || maxHeight <= 0 || maxWidth <= 0) { 78 | return new int[]{0, 0}; 79 | } 80 | if (width <= maxWidth && height <= maxHeight) { 81 | int[] size = new int[]{width, height}; 82 | return size; 83 | } else { 84 | int newWidth = 0; 85 | int newHeight = 0; 86 | if (width > maxWidth && height > maxHeight) { 87 | double ratio = (double) maxWidth / width; 88 | newWidth = Double.valueOf(width * ratio).intValue(); 89 | if (Double.valueOf(height * ratio).intValue() < maxHeight) { 90 | newHeight = Double.valueOf(height * ratio).intValue(); 91 | } else { 92 | newHeight = maxHeight; 93 | } 94 | } else if (width > maxWidth) { 95 | newWidth = maxWidth; 96 | double ratio = (double) maxWidth / width; 97 | newHeight = Double.valueOf(height * ratio).intValue(); 98 | } else if (height > maxHeight) { 99 | newHeight = maxHeight; 100 | double ratio = (double) maxHeight / height; 101 | newWidth = Double.valueOf(width * ratio).intValue(); 102 | } 103 | int[] size = new int[]{newWidth, newHeight}; 104 | return size; 105 | } 106 | } 107 | 108 | public static BufferedImage addImage(String fileLocation) { 109 | if (fileLocation == null || fileLocation.equals("")) { 110 | throw new RuntimeException("文件名不能为空"); 111 | } 112 | String suffix = fileLocation.substring(fileLocation.lastIndexOf(".") + 1).toLowerCase(); 113 | if (!("png").equals(suffix) && !("gif").equals(suffix) 114 | && !("jpeg").equals(suffix) && !("jpg").equals(suffix) && !("bmp").equals(suffix)) { 115 | return null; 116 | } 117 | BufferedImage bufferedImage = null; 118 | InputStream is = null; 119 | FileInputStream fileInputStream = null; 120 | try { 121 | File file = new File(fileLocation); 122 | fileInputStream = new FileInputStream(file); 123 | is = new BufferedInputStream(fileInputStream); 124 | String mimeType = URLConnection.guessContentTypeFromStream(is); 125 | if(mimeType.equals("image/gif")||mimeType.equals("image/x-bitmap")|| 126 | mimeType.equals("image/png")||mimeType.equals("image/jpeg")){ 127 | bufferedImage = ImageIO.read(file); 128 | return bufferedImage; 129 | }else { 130 | throw new RuntimeException("MIME不是图片文件"); 131 | } 132 | } catch (IOException e) { 133 | e.printStackTrace(); 134 | } finally { 135 | try { 136 | fileInputStream.close(); 137 | is.close(); 138 | } catch (IOException e) { 139 | } 140 | return bufferedImage; 141 | } 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/main/java/tony/ImgCanChoose.java: -------------------------------------------------------------------------------- 1 | package tony; 2 | 3 | import java.io.File; 4 | import javax.swing.filechooser.FileFilter; 5 | 6 | public class ImgCanChoose extends FileFilter{ 7 | 8 | @Override 9 | public boolean accept(File pathname) { 10 | String name=pathname.getName(); 11 | return name.toLowerCase().endsWith(".png"); 12 | } 13 | 14 | @Override 15 | public String getDescription() { 16 | return "*.PNG图片"; 17 | } 18 | } -------------------------------------------------------------------------------- /src/main/java/tony/StringBinUtil.java: -------------------------------------------------------------------------------- 1 | package tony; 2 | import java.io.UnsupportedEncodingException; 3 | 4 | public class StringBinUtil { 5 | public static byte[] strToByteArray(String str) { 6 | if (str == null) { 7 | return null; 8 | } 9 | byte[] byteArray = new byte[0];////-127 -123 -91 10 | try { 11 | byteArray = str.getBytes("utf8");//中文转换成byte类型后在Java里是负数 12 | } catch (UnsupportedEncodingException e) { 13 | e.printStackTrace(); 14 | } 15 | return byteArray; 16 | } 17 | 18 | public static String byteArrayToString(byte[] byteArray) { 19 | StringBuilder builder = new StringBuilder(byteArray.length); 20 | String zeroFill[] = new String[]{"0", "00", "000", "0000", "00000", "000000", "0000000"}; 21 | for (int i = 0; i < byteArray.length; i++) { 22 | String binString = Integer.toBinaryString(byteArray[i] & 0xff); 23 | int binLength = binString.length(); 24 | if (binLength < 8) { 25 | int strLen = 8 - binLength - 1; 26 | String fill = zeroFill[strLen]; 27 | builder.append(fill); 28 | } 29 | builder.append(binString); 30 | } 31 | return builder.toString();//0110100001100101011011000110110001101111111010001011111010010011111001011000010110100101 32 | } 33 | 34 | public static String intToBinStr(int len){ 35 | String binString = Integer.toBinaryString(len); 36 | int binLength = binString.length(); 37 | String fill = ""; 38 | if (binLength < 15) { 39 | int strLen = 15 - binLength; 40 | for (int i=0;istr.length()?str.length():end; 58 | String iter = str.substring(i, realEnd); 59 | int val = Integer.parseInt(iter, 2); 60 | byte b = (byte) val; 61 | byteArray[byteLength] = b; 62 | byteLength++; 63 | } 64 | } 65 | return byteArray; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/tony/TxtCanChoose.java: -------------------------------------------------------------------------------- 1 | package tony; 2 | 3 | import java.io.File; 4 | import javax.swing.filechooser.FileFilter; 5 | 6 | public class TxtCanChoose extends FileFilter{ 7 | @Override 8 | public boolean accept(File pathname) { 9 | String name=pathname.getName(); 10 | return name.toLowerCase().endsWith(".txt")||name.toLowerCase().endsWith(".java")|| 11 | name.toLowerCase().endsWith(".ini")||name.toLowerCase().endsWith(".properties")|| 12 | name.toLowerCase().endsWith(".md")||name.toLowerCase().endsWith(".yml")|| 13 | name.toLowerCase().endsWith(".html")||name.toLowerCase().endsWith(".htm")|| 14 | name.toLowerCase().endsWith(".xml")||name.toLowerCase().endsWith(".csv")||name.toLowerCase().endsWith(".sh"); 15 | } 16 | 17 | @Override 18 | public String getDescription() { 19 | return "*.文本"; 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/resources/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iminto/hideImage/cfe172bc989dbf54e634f933151e426d0599857e/src/main/resources/icon.png -------------------------------------------------------------------------------- /static/hide.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iminto/hideImage/cfe172bc989dbf54e634f933151e426d0599857e/static/hide.png -------------------------------------------------------------------------------- /static/yaofan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iminto/hideImage/cfe172bc989dbf54e634f933151e426d0599857e/static/yaofan.png -------------------------------------------------------------------------------- /static/yaofan_hide.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iminto/hideImage/cfe172bc989dbf54e634f933151e426d0599857e/static/yaofan_hide.png --------------------------------------------------------------------------------