PDA

View Full Version : compression for images?


misty83
04-21-2009, 02:31 AM
are there any methods or is it possible somehow to set a compression to the encodedImage (jpg/png)?

kind regards

pravin
04-21-2009, 08:38 AM
Try this -


/**
* Uncompress a previously compressed string;
* this method is the inverse of the compress method.
* @<hidden> byte array containing compressed data
* @<hidden> uncompressed string
* @<hidden> IOException if the inflation fails
*/
public static final byte[] uncompress(final byte[] compressed) throws IOException {
// String uncompressed = "";
byte[] result = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
GZIPInputStream zis = new GZIPInputStream(bais);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int numBytesRead = 0;
byte[] tempBytes = new byte[DEFAULT_BUFFER_SIZE];
while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) {
baos.write(tempBytes, 0, numBytesRead);
}

result = (baos.toByteArray());
} catch (Exception e) {
System.out.println("Error while unzip " + e.toString());
}

return result;
}





/**
* Compress a string
* @<hidden> uncompressed string
* @<hidden> byte array containing compressed data
* @<hidden> IOException if the deflation fails
*/
public static final byte[] compress(final byte[] uncompressed) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream zos = new GZIPOutputStream(baos);

byte[] uncompressedBytes = uncompressed;

zos.write(uncompressedBytes, 0, uncompressedBytes.length);
zos.close();

return baos.toByteArray();
}

misty83
04-22-2009, 01:59 AM
i was asking rather about image compression Image compression - Wikipedia, the free encyclopedia (http://en.wikipedia.org/wiki/Image_compression)

i do not need anything to be zipped and then unzipped. actually i even cannot do that. cause the idea is to make image compression so that it will be smaller, then send it to the server. i do not want to do pack-unpack. cause on the server side i cannot unpack anything. anyway, that is not the idea. You presented the code to pack-unpack, not the real image compression.
anyway, thanks i think Your code maybe usefull for me in the future.

kind regards.

ps.
anyone knows smth about image compression in bb?