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
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000
Accepted Solution

problem with comparing bitmaps

hi,

i've got a timerTask which every second makes a screenshot. then it compares it to the previousScreenshots -if they are equal, nothing happens, if they are different-the new screenshot is send to the server. here is few lines of how i do it (and also compare):

public void run(){ _width = Display.getWidth(); _height = Display.getHeight(); _currentScreen = new Bitmap(_width, _height); _previousScreen = new Bitmap(_width, _height); if( _currentScreen != null) { Bitmap _temp = _previousScreen; _previousScreen = _currentScreen; _currentScreen = _temp; } try{ Display.screenshot(_currentScreen); } catch(Exception e) { } //if bitmaps are equal-do nothing if( checkIfEqual(_currentScreen, _previousScreen)) { } //otherwise send screenshot else{ //send screenshot _singletonConnection.sendData(_currentScreen); } }//end of run // check if current and previous bitmaps are equal. // if yes - return true, otherwise return false public boolean checkIfEqual(Bitmap _b1, Bitmap _b2){ _rgbdata1 = new int[_width*_height]; _rgbdata2 = new int[_width*_height]; //getARGB _b1.getARGB(_rgbdata1, 0, _width, 0, 0, _width, _height); _b2.getARGB(_rgbdata2, 0, _width, 0, 0, _width, _height); //check, if ARGB's are equal if(!Arrays.equals(_rgbdata1, _rgbdata2)){ return false; //bitmaps are different } else { return true; //bitmaps are equal } }//end of checkIfEqual

 

 

 

my problem is that screenshots are always different-as i create a new object every time in run method ( _currentScreen = new Bitmap....);

but i must do it like this. cause if i'll define width/heigth and _currenScreen and _previousScreen in a constructor then - i'll have problem in comparing them when i turn around (for example in horizontal position) the device (for example Storm). so with every 'run' of run method i must define width, height, currentScreen and previousScreen so that i'll be able to recognize if the device is in vertical or horizontal position...

can anyone suggest me how can i solve it?

 

 kind regards

Developer
Posts: 304
Registered: ‎08-01-2008
My Device: Not Specified

Re: problem with comparing bitmaps

You are checking for each and every pixel to be equal Smiley Surprised

 

I think you should allow an error margin of at least 1%.

 

In any case the required internet connectivity speed of 480x360x4 =  691200 bytes/second ~ 700kbps is a hell of a speed that your application wants to use :-)

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: problem with comparing bitmaps

I think you don't need to initilize your _previousScreen in the run method.

 

You can try with the following:

   

 1. rather than using this line:

       _previousScreen = new Bitmap(_width, _height);

    in the your last line of run nethod add:

       _previousScreen = _currentScreen;

 

2. Add the follwiing lines at the start of checkIfEqual(...)

public boolean checkIfEqual(Bitmap _b1, Bitmap _b2) { if(_b1 == null || _b2 == null) return false; if(_b1.getWidth() != _b2.getWidth() || _b1.getHeight() != _b2.getHeight()) return false; // ................ // ................. }

 

Give it a try. This might help you.

 

 

Regards

Bikas

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: problem with comparing bitmaps

this makes that screenshots are not send at all.. it seems that it using this code _currentScreen and _previousScreen are always the same..
Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: problem with comparing bitmaps

Try somthing like this:

 

 

public void run() { _width = Display.getWidth(); _height = Display.getHeight(); _currentScreen = new Bitmap(_width, _height); try{ Display.screenshot(_currentScreen); } catch(Exception e) { } //if bitmaps are equal-do nothing if( checkIfEqual(_currentScreen, _previousScreen)) { } //otherwise send screenshot else{ //send screenshot _singletonConnection.sendData(_currentScreen); } _previousScreen = _currentScreen; }//end of run // check if current and previous bitmaps are equal. // if yes - return true, otherwise return false public boolean checkIfEqual(Bitmap _b1, Bitmap _b2) { if(_b1 == null || _b2 == null) return false; if(_b1.getWidth() != _b2.getWidth() || _b1.getHeight() != _b2.getHeight()) return false; _rgbdata1 = new int[_width*_height]; _rgbdata2 = new int[_width*_height]; //getARGB _b1.getARGB(_rgbdata1, 0, _width, 0, 0, _width, _height); _b2.getARGB(_rgbdata2, 0, _width, 0, 0, _width, _height); //check, if ARGB's are equal if(!Arrays.equals(_rgbdata1, _rgbdata2)){ return false; //bitmaps are different } else { return true; //bitmaps are equal } }//end of

 

 

 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: problem with comparing bitmaps

[ Edited ]

Well there is definitly a problem with initializing two new bitmaps every execution of run(). You are resetting both references which means when you do the swapping directly afterwards, you are swapping two empty bitmaps which is not doing anything. Also creating two new bitmaps every second will use up memory. You can limit that later.

 

 

public void run() {

try {

updateBitmap();
}
catch (Exception e) {

// log error
}
}

private void updateBitmap() throws Exception {

_previousScreen = _currentScreen;

 

int width = Display.getWidth();
int height = Display.getHeight();

_currentScreen = new Bitmap( width, height );

 

Display.screenshot( _currentScreen );

if ( _previousScreen != null ) {

if ( !checkIfEqual( _currentScreen, _previousScreen ) ) {

_singletonConnection.sendData( _currentScreen );
}
}
}

private boolean checkIfEqual( Bitmap bitmap1, Bitmap bitmap2 ) {

int[] argb1 = getARGB( bitmap1 );
int[] argb2 = getARGB( bitmap2 );
return Arrays.equals( argb1, argb2 );
}

private int[] getARGB( Bitmap bitmap ) {

int width = bitmap.getWidth();
int height = bitmap.getHeight();

int[] rgbdata = new int[width * height];
bitmap.getARGB( rgbdata, 0, width, 0, 0, width, height );

return rgbdata;
}

 

 

btw, I didn't run this but it should be okay.
Message Edited by mreed on 06-02-2009 01:55 PM
Highlighted
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: problem with comparing bitmaps

this is the same code, You presented in a previous post-but now i realized that i have another 'if' in run() method, which i did not type here. and i add a line _previousScreen = _currentScreen; out of this 'if', when i should put it in 'if'. so, now it works!!! Smiley Happy))

 

 

thank You very much for Your time and help!

  kind regards Smiley Happy)