| Reply | « Previous Thread | Next Thread » |
|
Hi,
i want to create pixels array from a png that i can put in a binary file. I want to use this binary file in my jar instead of using a png file. Is there any tool that convert a png to an array of shorts? Thanks. |
|
Do some search on the forum. There were several topics describing different ways of doing this
|
|
zavagoo,
I searched for a tool that would convert a png file into an array of shorts but had no success. I'm gonna start building my own tool for this purpose.... hopefully won't take too long. If I get it working soon I'll post the code here. Cheers. |
|
Well, we had this discussion a long time ago but now I can't find that thread, but the idea is to use the emulator.
The DirectGraphics.getPixels() function can already get the pixels from a picture in exactly the format you need. If you take the byte array it returns and save it to the RMS using the emulator, then you just have to open the RMS file on your computer (usually in a "jam-apps" directory or something like that, under the application's directory), strip a few RMS headers from it and you're done. shmoove |
|
Do you remember how many bytes the RecordStore headers were?
|
| rikard_wigforss@hotmail.com |
| View Public Profile |
| Find all posts by rikard_wigforss@hotmail.com |
|
Nope. Sorry. But if you right some dummy byte arrays with values you can easily pick out in the RMS and then look at the file with a hex editor you can probably find that out easily.
shmoove |
|
Hi,
thanks to all for your answers, especially shmoove and smb101. If you know about a j2se tool that can generate raw data from a png file, i'm interested. Or even if you can code such a tool (i mean smb101 ;-)), tell me! In my task list, at one moment i'll have to use such a tool. So if i can't find one, i think i'll try to code it myself, even if i don't know anything about bitmaps, png and graphic datas... Thank you again folks! See you. |
|
zavagoo,
Here's some rough code I done last night.... worked with a 32x32 png sprite but will still need some work but basically I used the method described by shmoove. 1) load the .png file 2) grab the pixels 3) write these pixels to the RMS (*** stage 1-3 is done using function grabImagePixels in the code below) 4) find the rms file on you HDD and manually remove the header from the RMS file. Once I had run the code on the emulator I found the RMS file in "c:\Nokia\Devices\Nokia_7210_SDK_v1_0\bin\jam-apps\(project)\RMS". If you edit this and search for the string "*** START HERE ***" and delete everything before and including this. This is to remove the RMS header information which is of no use to you. In addition it seems there may be some record header info(or something like that) so you have to delete some more data from the start. 5) add this new file as a resource in your project 6) use func readPNGBytes to load the file and populate the short[] pixel array. Sorry about the state of the code... as I said I done it late last night so I didn't have time to tidy up the code. hopefully you'll be able to use these funcs as a starting point. Any questions then please pose them here and I'll get back to you asap. Cheers import java.io.IOException; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import com.nokia.mid.ui.*; import java.io.*; import javax.microedition.io.*; public class MainCanvas extends FullCanvas { protected short imgPixels[]; // orig pixel aray protected short readPixels[]; // file copy pixel array protected int width, height; public MainCanvas() { width = 0; height = 0; } //****************************************************************************** public void start(MyBackgroundMain MIDlet) { grabImagePixels("test.png"); // loads the img into imgPixels and sets width and height vals readPNGBytes("OutPut.txt", width, height); // reads the file and puts pixels into readPixels // will be the same dim as the original sprite Display.getDisplay(MIDlet).setCurrent(this); } //****************************************************************************** public void paint(Graphics g) { // blank screen g.fillRect(0,0,128,128); DirectGraphics dg = DirectUtils.getDirectGraphics(g); // draw original pixel array dg.drawPixels(imgPixels, false, 0, width, 0, 0, width, height, 0, DirectGraphics.TYPE_USHORT_4444_ARGB); g.setColor(0xAAAAAA); g.drawString("Original", 40, 10, g.TOP|g.LEFT); // draw pixel array from file dg.drawPixels(readPixels, false, 0, width, 0, 50, width, height, 0, DirectGraphics.TYPE_USHORT_4444_ARGB); g.setColor(0xAAAAAA); g.drawString("Copy", 40, 60, g.TOP|g.LEFT); } //****************************************************************************** public void grabImagePixels(String file) { Image mainImg=null, LoadImg=null; // load orig img try { LoadImg = Image.createImage("/"+file); height = LoadImg.getHeight(); width = LoadImg.getWidth(); System.out.println("LOAD = "+file); } catch (IOException e) { System.out.println("ERROR! /"+file+" Failure"); } // grab pixel array imgPixels = new short[width*height]; mainImg = DirectUtils.createImage(width, height, 0); DirectGraphics dg = DirectUtils.getDirectGraphics(mainImg.getGraphics()); dg.drawImage(LoadImg, 0, 0, Graphics.TOP | Graphics.LEFT, 0); dg.getPixels(imgPixels, 0, width, 0, 0, width, height, DirectGraphics.TYPE_USHORT_4444_ARGB); // write pixel array to RMS PersistantData store; store = new PersistantData(); store.writeToStore(width, height, imgPixels); // don't need the images now. mainImg=null; LoadImg=null; System.gc(); } //****************************************************************************** void readPNGBytes(String filename, int width, int height) { // load sprite from resource System.out.println("LOAD PNG BYTES = "+ filename); InputStream is = this.getClass().getResourceAsStream("/"+filename); int totalSize = width * height; readPixels = new short[totalSize]; try { for (int i = 0; i < totalSize; i++) { // read 2 bytes at a time which make up 1 short value byte one = (byte)is.read(); byte two = (byte)is.read(); readPixels[i] = (short) ((one << 8) + two); } } catch (IOException e) {} } //****************************************************************************** } /* This file was created by Nokia Developer's Suite for J2ME(TM) */ import java.util.*; import javax.microedition.rms.*; public class PersistantData { private RecordStore recordStore; static private final String RECORD_LABEL = "PNGOutput"; //****************************************************************************** public PersistantData() { } //****************************************************************************** private void initStore() { try { recordStore = RecordStore.openRecordStore(RECORD_LABEL, true); if (recordStore.getNumRecords() > 0) { // delete and start again recordStore.closeRecordStore(); recordStore.deleteRecordStore(RECORD_LABEL); recordStore = RecordStore.openRecordStore(RECORD_LABEL, true); } } catch (RecordStoreException e) { e.printStackTrace(); closeStore(); } } //****************************************************************************** public void writeToStore(int w, int h, short[] pixels) { System.out.println("writeStore()"); initStore(); try { int startPos=0; int srcSize = pixels.length; byte[] newArray = new byte[srcSize*2]; //2 bytes for 1 short for (int i=0; i<srcSize; i++) { newArray[startPos] = (byte) ((pixels[i] & 0xFF00) >> 8); // high byte first newArray[startPos+1] = (byte) (pixels[i] & 0x00FF); // low byte next startPos+=2; } String startText = "***START HERE***"; recordStore.addRecord(startText.getBytes(), 0, startText.length()); recordStore.addRecord(newArray, 0, newArray.length); } catch (RecordStoreException e) { e.printStackTrace(); closeStore(); } closeStore(); } //****************************************************************************** private void closeStore() { try { recordStore.closeRecordStore(); } catch (RecordStoreException e) { e.printStackTrace(); } } //****************************************************************************** } |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|