Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
bog
Contributor
Posts: 35
Registered: ‎05-11-2011
My Device: 9800
My Carrier: bell

byte matrix to bitmap

how to display the generated QR code by zxing on BB screen?

 

ByteMatrix barcode = qrCode.getMatrix();

 

when you have bytematrix return by zxing, how to convert it to bitmap so that I can show it...

 

Anyone know how to do it?

Thanks!

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: byte matrix to bitmap

Based on the docs for it I don't think you can just convert it straight to a Bitmap. It stores its data in a 2 dimensional array with data only being -1, 0, or 1. I suppose you could get the array of bytes, parse through it and assign colors to the 3 values, and then create your own Bitmap from the resulting byte[] that you create.

bog
Contributor
Posts: 35
Registered: ‎05-11-2011
My Device: 9800
My Carrier: bell

Re: byte matrix to bitmap

Do you mean something like this?
private static Bitmap ByteMatrix2Bitmap(ByteMatrix matrix){
				int width = matrix.getWidth(); 
				int height = matrix.getHeight(); 
	            byte[][] array = matrix.getArray(); 
	            
	            byte[] imgdata = new byte[width * height];
	            for (int y = 0; y < height; y++) { 
	            	for (int x = 0; x < width; x++) { 
	            		if (array[y][x]==0)
	            			imgdata[y * width + x] = 0;
	            		else
	            			imgdata[y * width + x] = 1;
	            	} 
	            }
	            
				Bitmap bitmap =  Bitmap.createBitmapFromBytes(imgdata,0,imgdata.length,1);
				return bitmap;
	}

but why the Bitmap.createBitmapFromBytes(imgdata,0,imgdata.length,1) throw exception?

 

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: byte matrix to bitmap

You may want to change your

if (array[y][x]==0)
	            			imgdata[y * width + x] = 0;
	            		else
	            			imgdata[y * width + x] = 1;

to actual colors (use Color.BLACK for example). Also, what Exception (name and message) is getting thrown?

bog
Contributor
Posts: 35
Registered: ‎05-11-2011
My Device: 9800
My Carrier: bell

Re: byte matrix to bitmap

(byte)color.black is still the same...

 

It is java.lang.illegalArgumentException.

 

Thanks!

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: byte matrix to bitmap

You might have better luck using the constructor Bitmap(int type, int width, int height, byte[] data). You won't be able to just throw the flattened version that you have in it, but you can probably come up with a transformation (bit shifts)

bog
Contributor
Posts: 35
Registered: ‎05-11-2011
My Device: 9800
My Carrier: bell

Re: byte matrix to bitmap

Thanks, it seems to work. 

I just do not know the transformation rule, the bit matrix I want to convert is simple, just 0 and 1. 

Anyone know the fransformation rule?

bog
Contributor
Posts: 35
Registered: ‎05-11-2011
My Device: 9800
My Carrier: bell

Re: byte matrix to bitmap

what the input byte array for function createBitmapFromBytes should looks like?

bog
Contributor
Posts: 35
Registered: ‎05-11-2011
My Device: 9800
My Carrier: bell

Re: byte matrix to bitmap

	int width = matrix.getWidth(); 
	int height = matrix.getHeight();
        //matrix is a 0-1 matrix
        byte[][] array = matrix.getArray(); 
        int[] imgdata = new int[width*height];
        Bitmap bitmap  = new Bitmap(width, height); 
        
        for (int y = 0; y < height; y++) { 
        	for (int x = 0; x< width; x++){
        		if (array[y][x] == 0)
        				imgdata[y * width + x] = Bitmap.TRUE_WHITE;
        		else
        				imgdata[y * width + x] = Bitmap.TRUE_BLACK;
        		}
        }
        bitmap.setARGB(imgdata, 0, width, 0, 0, width, height);
		Bitmap retmap = new Bitmap(10*width ,10*height );
		bitmap.scaleInto(retmap, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT);	
		return retmap;

  Now, I can use the function here to get a blurred image of the 0-1 matrix(retmap). Any reason it is blurred? When I try to visualize small Bitmap bitmap, it appears to be blank, although its values are not. Any guess about what is wrong here?

 

Thanks!