在 java 类中隐藏图像的一种相当复杂的解决方案,不包括文件本身或使用 base64(从而减少类 java 文件大小),是使用以下 java 程序工具将图像转换为 java 字节数组的集合以及将组装原始图像的方法调用:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
public class ImageDataGenerator {
public static void main(String[] params) throws InterruptedException, FileNotFoundException, IOException {
if (params.length != 2) {
System.out.printf("Uso: java ImageDataGenerator <nombre_imagen> <archivo_imagen>\n");
System.exit(1);
}
String imageName = params[0];
FileInputStream fis = new FileInputStream(params[1]);
PrintWriter p = new PrintWriter("imgData.txt");
p.printf(" private byte[] get%sPart1() {\n", imageName);
p.printf(" return new byte[] {");
int readed = 0;
int writed = 0;
int numMethods = 1;
while ((readed = fis.read()) != -1) {
if (writed < 3000) {
p.printf("%d,", (byte) readed);
} else {
writed = 0;
p.printf("%d};\n", (byte) readed);
p.printf(" }\n");
numMethods++;
p.printf(" private byte[] get%sPart%d() {\n", imageName, numMethods);
p.printf(" return new byte[] {\n ");
}
if ((++writed % 500) == 0) {
p.printf("\n");
}
}
p.printf("};\n");
p.printf(" }\n\n");
p.println(" private BufferedImage createImageFromArrays(byte[]... arrays) throws IOException { ");
p.println(" int size = 0;");
p.println(" for (byte[] array : arrays) size = array.length;");
p.println(" byte[] imageInByte = new byte[size];");
p.println(" int pos = 0;");
p.println(" for (byte[] array : arrays) {");
p.println(" System.arraycopy(array, 0, imageInByte, pos, array.length);");
p.println(" pos += array.length;");
p.println(" }");
p.println(" ");
p.println(" InputStream in = new ByteArrayInputStream(imageInByte);");
p.println(" return ImageIO.read(in);");
p.println(" }");
p.println("\n");
p.println("// Insert this call anywhere in your code");
p.printf("createImageFromArrays(");
for (int i = 1; i <= numMethods; i++)
p.printf("get%sPart%d()%s ", imageName, i, i <= numMethods - 1 ? "," : "");
p.printf(");");
p.flush();
p.close();
}
}
使用工具键入:java ImageDataGenerator hiddenImage /path/to/image/image.png
它将生成一个文件,命名imgData.txt
您必须在 JFrame
类声明部分中复制/粘贴的内容。生成文件的最后一行将包含一个名为的方法的createImageFromArrays
调用,您必须在JFrame
onShow/onLoad 事件侦听器中调用该方法
(我以前用这个工具来隐藏 J2ME 应用程序中的图像/资源,希望对你有帮助)