You Are Here:

Community: Developer Discussion Boards

#1 Old Working with PNG Images - loading , saving , reading - 2009-03-21, 08:08

Join Date: Mar 2009
Posts: 15
veenit33
Offline
Registered User
Hello friends

I am creating a project in J2ME which requires me to select a PNG image from the filesystem of the mobile phone

I load this image in memory

I want to know whether the PNG file data is compressed or no?

When i use the Image.getRGB() method i will be getting the int[] in hand.

Also how do i proceed converting the int[] to byte[]?

I want to use this byte[] to hide bits of data in it..

Actually i am performing Image Steganography in mobile phones.

So whatever text the user enters will be hidden behind the image.
I am using the LSB technique of hiding data ie in each pixel i will be hiding 1 bit.

What is a PNG Encoder?
Also i would like to know will i be needing the PNG Encoder?


Til now what ever image i create i cannot read that back from my application
So if i could get any help from u will be appreciable.
Reply With Quote

#2 Old Re: Working with PNG Images - loading , saving , reading - 2009-03-21, 08:11

Join Date: Feb 2009
Posts: 1
niujianwei's Avatar
niujianwei
Offline
Forum Nokia Champion
You can use a tool to convert a BMP file into a PNG format file.
Reply With Quote

#3 Old Re: Working with PNG Images - loading , saving , reading - 2009-03-21, 08:19

Join Date: Mar 2009
Posts: 15
veenit33
Offline
Registered User
I dont need to convert a BMP file to a PNG file
I want to work with PNG images.

My problem is how do i get the byte[] data of PNG file so dat i can modify it according to my need.

What are the steps in doin so...?
Reply With Quote

#4 Old Thumbs up Re: Working with PNG Images - loading , saving , reading - 2009-03-23, 10:38

Join Date: Sep 2008
Posts: 1,195
Location: DELHI
Send a message via Yahoo to jitu_goldie
jitu_goldie's Avatar
jitu_goldie
Offline
Forum Nokia Champion
Hi,
Please try out the following link. there is something for ur problem:

http://www.j2meforums.com/forum/inde...;topic=20468.2


hope this will help u somewhile..

thanks,
jitu_goldie..


thanks,
jitu_goldie..

KEEP TRYING..
Reply With Quote

#5 Old Re: Working with PNG Images - loading , saving , reading - 2009-03-23, 13:31

Join Date: Mar 2008
Posts: 198
mikemoore
Offline
Regular Contributor
A much better solution to this is at http://www.java-tips.org/java-me-tip...-from-rms.html

- Mike
NAVTEQ Network for Developers
The community for developing innovative location-based applications
http://NN4D.com
Reply With Quote

#6 Old Re: Working with PNG Images - loading , saving , reading - 2009-03-23, 15:47

Join Date: Mar 2009
Posts: 15
veenit33
Offline
Registered User
Hello jitu

i am modifying the bits of the PNG data in byte[] and then creating a datachunk.

i use this data chunk to create a byte[] which contains the signature header trailer.

How do i go back to find out as to where did i start modifying the bits

the PNG encoder i am using is as follows:





/*
* Minimal PNG encoder to create MIDP images from RGBA arrays.
*
* Copyright 2006 Christian Froeschlin
*
* Changelog:
*
* 09/22/08: Fixed Adler checksum calculation and byte order
* for storing length of zlib deflate block. Thanks
* to Miloslav R?ži?ka for noting this.
*
* www.chrfr.de
*
*/

import java.io.*;
import javax.microedition.lcdui.Image;

public class PNG
{

public static Image toImage(int width, int height, byte[] alpha, byte[] red, byte[] green, byte[] blue)
{
try
{
byte[] png = toPNG(width, height, alpha, red, green, blue);
return Image.createImage(png, 0, png.length);
}
catch (IOException e)
{
return null;
}
}

public static byte[] toPNG(int width, int height, byte[] alpha, byte[] red, byte[] green, byte[] blue) throws IOException
{
byte[] signature = new byte[] {(byte) 137, (byte) 80, (byte) 78, (byte) 71, (byte) 13, (byte) 10, (byte) 26, (byte) 10};
byte[] header = createHeaderChunk(width, height);
byte[] data = createDataChunk(width, height, alpha, red, green, blue);
byte[] trailer = createTrailerChunk();

ByteArrayOutputStream png = new ByteArrayOutputStream(signature.length + header.length + data.length + trailer.length);

png.write(signature);
png.write(header);
png.write(data);
png.write(trailer);
return png.toByteArray();
}

public static byte[] toPNG(byte[] img_data,int width, int height, byte[] alpha, byte[] red, byte[] green, byte[] blue) throws IOException
{
byte[] signature = new byte[] {(byte) 137, (byte) 80, (byte) 78, (byte) 71, (byte) 13, (byte) 10, (byte) 26, (byte) 10};
byte[] header = createHeaderChunk(width, height);
byte[] data = img_data;
byte[] trailer = createTrailerChunk();

ByteArrayOutputStream png = new ByteArrayOutputStream(signature.length + header.length + data.length + trailer.length);

System.out.println("Signature Length === "+signature.length);
System.out.println("Header Length === "+header.length);
System.out.println("Trailer Length === "+trailer.length);

png.write(signature);
png.write(header);
png.write(data);
png.write(trailer);
return png.toByteArray();
}

public static byte[] createHeaderChunk(int width, int height) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(13);
DataOutputStream chunk = new DataOutputStream(baos);
chunk.writeInt(width);
chunk.writeInt(height);
chunk.writeByte(8); // Bitdepth
chunk.writeByte(6); // Colortype ARGB
chunk.writeByte(0); // Compression
chunk.writeByte(0); // Filter
chunk.writeByte(0); // Interlace
return toChunk("IHDR", baos.toByteArray());
}

public static byte[] createDataChunk(int width, int height, byte[] alpha, byte[] red, byte[] green, byte[] blue) throws IOException
{
int source = 0;
int dest = 0;
byte[] raw = new byte[4*(width*height) + height];
for (int y = 0; y < height; y++)
{
raw[dest++] = 0; // No filter
for (int x = 0; x < width; x++)
{
raw[dest++] = red[source];
raw[dest++] = green[source];
raw[dest++] = blue[source];
raw[dest++] = alpha[source++];
}
}
return toChunk("IDAT", toZLIB(raw));
}

public static byte[] createTrailerChunk() throws IOException
{
return toChunk("IEND", new byte[] {});
}

public static byte[] toChunk(String id, byte[] raw) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(raw.length + 12);
DataOutputStream chunk = new DataOutputStream(baos);

chunk.writeInt(raw.length);

byte[] bid = new byte[4];
for (int i = 0; i < 4; i++)
{
bid[i] = (byte) id.charAt(i);
}

chunk.write(bid);

chunk.write(raw);

int crc = 0xFFFFFFFF;
crc = updateCRC(crc, bid);
crc = updateCRC(crc, raw);
chunk.writeInt(~crc);

return baos.toByteArray();
}

static int[] crcTable = null;

public static void createCRCTable()
{
crcTable = new int[256];

for (int i = 0; i < 256; i++)
{
int c = i;
for (int k = 0; k < 8; k++)
{
c = ((c & 1) > 0) ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
crcTable[i] = c;
}
}

public static int updateCRC(int crc, byte[] raw)
{
if (crcTable == null)
{
createCRCTable();
}

for (int i = 0; i < raw.length; i++)
{
crc = crcTable[(crc ^ raw[i]) & 0xFF] ^ (crc >>> 8);
}

return crc;
}


// Creates a single zlib block contain a single
// uncompressed deflate block. Must be < 64K!
public static byte[] toZLIB(byte[] raw) throws IOException
{
byte tmp;
ByteArrayOutputStream baos = new ByteArrayOutputStream(raw.length + 5 + 6);
DataOutputStream zlib = new DataOutputStream(baos);
tmp = (byte) (8 + (7 << 16));
zlib.writeByte(tmp); // Compression + Flags
zlib.writeByte((31 - ((tmp << 8) % 31)) % 31); // FCHECK (compr/dict 0)

//Uncompressed deflate block
zlib.writeByte((byte) 1); //Final flag set, Compression type 0
char length = (char) raw.length;
zlib.writeByte((byte)(length & 0xFF)); //Length LSB
zlib.writeByte((byte)((length & 0xFF00) >> 8)); //Length MSB
zlib.writeByte((byte)(~length & 0xFF)); //Length 1st complement LSB
zlib.writeByte((byte)((~length & 0xFF00) >> 8)); //Length 1st complement MSB
zlib.write(raw); //Data

// zlib block check sum
zlib.writeInt(calcADLER32(raw));
return baos.toByteArray();
}

// Unverified (the Java PNG loader did not complain at any value)
public static int calcADLER32(byte[] raw)
{
int s1 = 1;
int s2 = 0;
for (int i = 0; i < raw.length; i++)
{
int abs = raw[i] >=0 ? raw[i] : (raw[i] + 256);
s1 = (s1 + abs) % 65521;
s2 = (s2 + s1) % 65521;
}
return (s2 << 16) + s1;
}
}
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving Captured images in PNG format dar7ren General Symbian C++ 3 2006-12-09 17:19
Saving images on the phone (PNG, JPG...) rikow Mobile Java Media (Graphics & Sounds) 7 2006-12-08 08:35
help about reading png file gzfan Symbian Media (Graphics & Sounds) 4 2005-11-24 15:04
saving images as PNG sopta007 General Symbian C++ 2 2004-02-12 11:03
Loading Images after game has been installed ajay_sipl Mobile Java General 2 2002-12-17 08:13

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d134434X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ