Problem displaying generated RGBImage in ImageViewer

30 views
Skip to first unread message

Eric Vortriede

unread,
Jun 15, 2014, 3:39:35 PM6/15/14
to codenameone...@googlegroups.com
I am generating QR Codes as RGBImage objects using the following code:

    private Image getQRImage(String data, int w, int h)
   
{
     
try
     
{
       
BitMatrix matrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, w, h);
       
int width = matrix.getWidth();
       
int height = matrix.getHeight();
       
int[] image = new int[width*height];
       
int i=0;
       
for (int x = 0; x < width; x++) {
         
for (int y = 0; y < height; y++) {
            image
[i++] = !matrix.get(x, y) ? 0xffffffff : 0xff000000;
         
}
       
}
       
return new RGBImage(image, width, height);
     
}
     
catch (Exception e)
     
{
       
// TODO Auto-generated catch block
        e
.printStackTrace();
     
}
     
return null;
   
}




I can display the images that this method returns in a Dialog but when I try displaying them using an ImageViewer they do not display.  I have only attempted this in the simulator on Windows though I intend to have iOS, Android and Blackberry implementations.

The code that I am using to attempt to display the image looks like this:

      ImageViewer viewer = this.findWWWImageViewer();
     
Image i = getQRImage("hello", getWidth(), getHeight());
      viewer
.setImage(i);


Thanks much, in advance, for any assistance.

-Eric


Shai Almog

unread,
Jun 16, 2014, 2:03:31 AM6/16/14
to codenameone...@googlegroups.com
ImageViewer expects an EncodedImage and RGBImage isn't really an image but rather a set of integer values. This will require two steps, first convert the RGBImage to a standard image using:

Image.createImage(int[] rgb, int width, int height)

Now that you have an Image you can convert it to an encoded image using EncodedImage.createFromImage(Image i, boolean jpeg)

To understand the differences you can check out the developer guide section on images, there are WAY too many types and subtleties there but trust me when I say that this is a simplified version...

Eric Vortriede

unread,
Jun 17, 2014, 11:02:00 AM6/17/14
to codenameone...@googlegroups.com
Thanks Shai,

That took care of it.  If ImageViewer requires an EncodedImage, why not have the setImage method require that?  Or better yet have versions of setImage for each of the Image types which performs the appropriate conversion.

Thanks again,
Eric

Shai Almog

unread,
Jun 17, 2014, 11:52:03 AM6/17/14
to codenameone...@googlegroups.com
I thought it did, can you file an issue?

Eric Vortriede

unread,
Jun 17, 2014, 8:52:56 PM6/17/14
to codenameone...@googlegroups.com
Done.
Reply all
Reply to author
Forward
0 new messages