Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A rather complex solution to hide an image in a java class, without including the file itself or using base64 (thus reducing the class java file size), is to use the following java program tool to convert an image to a collection of java byte arrays and method invocations that will assemble the original image:</p> <pre><code>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 &lt;nombre_imagen&gt; &lt;archivo_imagen&gt;\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 &lt; 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 &lt;= numMethods; i++) p.printf("get%sPart%d()%s ", imageName, i, i &lt;= numMethods - 1 ? "," : ""); p.printf(");"); p.flush(); p.close(); } } </code></pre> <p>Use the tool typing: <code>java ImageDataGenerator hiddenImage /path/to/image/image.png</code></p> <p>It will generate a file named <code>imgData.txt</code> which content you must copy/paste inside your <code>JFrame</code> class declaration section. The last line of the generated file will contain a call to a method named <code>createImageFromArrays</code> which you must invoke in your <code>JFrame</code> onShow/onLoad event listener </p> <p>(I used this tool before to hide images/resources in J2ME applications, hope it helps)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload