iwp.setProgressionType("layer");
//Following is must to generate j2k JPEG 2000 stream
iwp.setWriteCodeStreamOnly(true);
iwp.setNumDecompositionLevels(5);
FileCacheImageOutputStream fileImageOutputStream = new FileCacheImageOutputStream(baos, null);
writer.setOutput(fileImageOutputStream);
//TODO changed
// writer.write(null, new IIOImage(bitmap, null, null), iwp);
// IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(bitmap), iwp);
J2KMetadata metadata = new J2KMetadata(bitmap.getColorModel(),
bitmap.getSampleModel(), bitmap.getWidth(), bitmap.getHeight(), iwp, writer);
writer.write(metadata, new IIOImage(bitmap, null, null), iwp);
// writer.write(bitmap);
fileImageOutputStream.flush();
fileImageOutputStream.close();
// ImageIO.write((BitmapBufferedImageImpl)bitmap, "jpg2000", baos);
byte[] bytes = baos.toByteArray();
baos.close();
return bytes;
}