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
Highlighted
New Developer
Posts: 17
Registered: ‎01-15-2009
My Device: Not Specified
Accepted Solution

how to remember bitmap

hi,
i've got a problem with bitmaps. i'm taking screenshots (the task is put in the timerTask)
and i have two bitmaps - where one is holding the current screenshot and the second one is supposed to hold the previous one. and here comes troubles. i thought that something like that may be right:

 

Bitmap prev;
Bitmap curr;

//later


prev = curr;
 

try{
 Display.screenshot(curr);
 
} catch(Exception e) { }

 

i thought that when prev=curr is before the block try-catch, then to the prev-bitmap is written the current one that was taken before. and then in the try-catch block, the new screen is put in the curr-bitmap. anyway, it seems that it
is put to the both of these objects. Can You suggest me how can i make that prev will remember the previous screenshot(taken a second before)?


 regards 

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

Re: how to remember bitmap

Those are just references to the objects. By doing prev = curr both prev and curr now reference the same object and unless you stored the reference prev held, you just lost that bitmap. Use a third value when switching references like that.

temp = prev;

prev = curr;

curr = temp;

New Developer
Posts: 17
Registered: ‎01-15-2009
My Device: Not Specified

Re: how to remember bitmap

hmm, actually i'm not sure what You are suggesting.. i mean-You want to put into temp, prev-object (witch at the beginning is null, cause this map should rembeber current-1, so at the beginning it is empty), then  to the previous, put the current one (so here i will get the same i got before-every previous=current) and then to the current, the temp one which is previous???

 

i switched bitmaps to ints to write a simple example with for-loop(to pretend a timer-task):

 

static int curr ;

static int prev;

static int temp;

 

 

 for (curr=0; curr<3; curr++){

 

 

temp = prev;

prev = curr;

curr = temp;System.out.println("curr "+curr+" prev "+prev+" temp "+temp);

 

}

 

 

then what i got is:

 

curr 0 prev 0 temp 0

curr 0 prev 1 temp 0

curr 1 prev 1 temp 1

curr 1 prev 2 temp 1

curr 2 prev 2 temp 2

 

New Developer
Posts: 17
Registered: ‎01-15-2009
My Device: Not Specified

Re: how to remember bitmap

and what i need is:

curr: 1,  prev: 0

curr:2,  prev:1

curr:3,  prev:2

curr:4,  prev:3

 

 

etc..

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to remember bitmap

In the example you have provided, the variables temp, prev and curr are primitives.  A Bitmap is an object.  Objects are referenced and this makes swapping values around a little less intuitive..

 

A good example is a String.

 

String _a = "abcd"

String _b = a;

_a = _a + "e".

 

After this, when you print the _a you get "abcde", which is also what you get when you print _b, because _a and _b are references to the same object.

 

Same principal applies to Bitmap.  What the poster assumed you had was something like

 

Bitmap prev = new Bitmap(...);  // appropriate to your display

Bitmap curr = new Bitmap(...); // as above

 

In this situation, if you code

prev = curr;

you have lost your reference to the Bitmap, that was previously referenced by prev. 

 

If however you do

Bitmap temp = prev;

prev = curr;

curr = temp;

then you have swapped the two around.

 

In this particular case, I think you have assumed that Display.screenshot(...) will return a new screenshot.  It doesn't, it overwrites the existing one.  I suspect your current code is a little like the following;

 

String _curr = "abc"

String _prev = curr;

_curr = "def";

 

Now _prev will print out "def".

New Developer
Posts: 17
Registered: ‎01-15-2009
My Device: Not Specified

Re: how to remember bitmap

thanks for Your reply.

still, i'm not sure if Your suggestions are correct. i modified my code:

 

private Bitmap _current ;

private Bitmap _previous ;

private Bitmap _temp;

 

public Constructor(){

 _current = new Bitmap(Display.getWidth(), Display.getHeight());

_previous = new Bitmap(Display.getWidth(), Display.getHeight());

_temp = new Bitmap(Display.getWidth(), Display.getHeight());

}

 

public
void run(){

 

 

if( _current != null) {

_temp = _previous;

_previous = _current;
 _current = _temp;

}

 

 

try{

 

 

Display.screenshot(_current);

Dialog.alert("current "+_current + "previous"+_previous);

}catch(Exception e){}

and what i got is:

 

_current: data1    _previous: data0

_current: data0   _previous: data1

_current: data1   _previous: data0

 

etc..

 

?

New Developer
Posts: 17
Registered: ‎01-15-2009
My Device: Not Specified

Re: how to remember bitmap

still, there are situations when previous=current. i mean-when the screen does not change. and i'm wondering what is happenning in my application - the only thing that is changing in the screen is that an alert (Dialog.alert("current"...)) shows. and it only shows switched data0-data1, data1-data0.. even if i'm changing the screen (for example by putting the application in the background) i still have the same switching values data0-data1, data1-data0...
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to remember bitmap

Sorry, you have lost me here.

 

I think you are doing your debugging using the following line:

Dialog.alert("current "+_current + "previous"+_previous);

 

Now this attempting to print a string representation that will identify the Bitmap.  When I try this I get:

net.....system.Bitmap@8636dacc

I presume this is what you mean by data0 and data1, I presume the to differ in the last few characters.

 

Now Bitmap doesn't have a toString(), so this will be using the standard Object toString() method, and so what you are seeing is the name plus the hashvalue - which on a BlackBerry seems to be the memory address.  Now, the memory address of an Object typically doesn't change,  So you are using a toString() method that does not change with the change in the content of the Object. 

 

The fact that it does not change is expected.  It does NOT mean that the contents have not changed.

 

To prove that the contents have changed, do a getARGB(..) on the two Bitmaps, and compare the returned array (make sure that you use two different arrays).

 

Does that make sense?

 

New Developer
Posts: 17
Registered: ‎01-15-2009
My Device: Not Specified

Re: how to remember bitmap

i understand, but this still does not work:

 

private int w = Display.getWidth();

private int h = Display.getHeight();

 

//later

 int[] _rgbdata1 = new int[w*h]; int[] _rgbdata2 = new int[w*h];

 

 

_currentScreen.getARGB(_rgbdata1, 0, w, 0, 0, w, h); _previousScreen.getARGB(_rgbdata2, 0, w, 0, 0, w, h);

 

 

if(_rgbdata1 != _rgbdata2 ){ Dialog.alert("different");

}

 

it show 'different' all the time. but i do not make any changes on the screen.. it is the same all the time. so, she screenshots (theirs rgbdata) should be the same, but are not..

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to remember bitmap

[ Edited ]

Not being rude, but I suggest that you get a good Java book, and review the first few chapters when they will talk about primitives, Objects, references to Object, comparing Objects etc. etc.

 

An array is also an object.  Its name is just a reference.  So the expression

if ( _araay1 != _array2 ) { ...}

will always be true if the names reference different Objects.

 

Again you want to compare the contents, not the reference.

 

You could code something like

boolean different = false;

for ( int i = 0; i < _array1.length; i++ ) {

if ( _array1[i] != _array2[i] ) {

different = true;

break;

}

}

if ( different) {  }

 

But Rim provide you with a very convenient Arrays class, so you just code

if ( !Arrays.equals(_array1, _array2 ) ) {....}

Message Edited by peter_strange on 01-17-2009 11:00 PM