Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Convert an Image to a Byte Array for Inclusion in Source Code  (Read 2478 times)
0 Members and 1 Guest are viewing this topic.
Offline noblemaster

JGO Ninja


Medals: 15
Projects: 7


Age of Conquest makes your day!


« Posted 2008-02-27 10:09:15 »

I think I saw someone post something similar before? Nevertheless, here we go again...

I am somewhat anal about storing icons (or small images) in source code rather than on the file system. It's very easy to mess up image loading, so here is my code to store an image in a .java source file rather than on the file system, where it can easily get lost. Please note the image size roughly doubles, so the procedure isn't feasible for larger images.

Also, obviously I am not doing this for every icon, but only for the ones that are very unlikely to change (e.g. play, stop, pause button icons etc.).

Example Conversion: Image: / Java Source Code: Byte Array
Resource/Download: ImageUtil.java

Convert a byte array to Java code:
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  
    // Load image
   Image image = loadImage(...);
   
    // Convert the image to a byte array
   byte[] data = ImageUtil.imageToByteArray(image);
   
    // Output data
   FileOutputStream fos = new FileOutputStream(args[1], false);
    DataOutputStream dos = new DataOutputStream(fos);
    for (int i = 0; i < data.length; i++) {
      if ((i % 8) == 0) {
        dos.writeBytes("\n");
      }
      dos.writeBytes("(byte)0x");
      String hex = Integer.toHexString(data[i]);
      if (hex.length() == 1) {
        hex = "0" + hex;
      }
      else if (hex.length() > 2) {
        hex = hex.substring(hex.length() - 2);
      }
      dos.writeBytes(hex);
      dos.writeBytes(", ");
    }
    dos.flush();
    dos.close();
    fos.flush();
    fos.close();


Website: http://www.noblemaster.com/public/ (see section 3)

Offline Abuse

JGO Coder


Medals: 2


falling into the abyss of reality


« Reply #1 - Posted 2008-02-27 10:19:08 »

While I'm sure someone will appreciate the contributed code, I wouldn't recommend such a practice.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (78 views)
2013-05-17 22:29:12

alaslipknot (89 views)
2013-05-16 22:24:48

gouessej (122 views)
2013-05-16 01:53:38

gouessej (114 views)
2013-05-16 01:17:58

theagentd (126 views)
2013-05-15 16:01:13

theagentd (113 views)
2013-05-15 16:00:54

StreetDoggy (158 views)
2013-05-14 16:56:26

kutucuk (180 views)
2013-05-12 18:10:36

kutucuk (179 views)
2013-05-12 16:36:09

UnluckyDevil (186 views)
2013-05-12 06:09:57
Complex number cookbook
by Roquen
2013-04-24 13:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 17:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 17:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 17:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 18:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 17:17:38

Java Data structures
by Roquen
2013-03-29 14:21:12

Topic Request
by kutucuk
2013-03-22 22:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.116 seconds with 20 queries.