Blackberry app is very slow on startup, how can i fix this? - blackberry

I recently updated my app to work on nearly all of the phones. I did this by having the first screen detect the screen size and then change all the images (there are a lot of images). As such, the startup on the non-base model phones is like 15 seconds, it looks like the phone is freezing but its just changing the images. It does this every time I open the app. What can I do to fix this?

Make sure that you are resizing images on a separate thread (no UI Blocking operation).
It would be better if you store all the resized images on persistent
storage so that you don't need to resize same image twice on a
handset.
[edited]
Some links about how to use persistence storage:
BlackBerry persistent store
Create a persistent data store
BlackBerry Java Application Development Guide
Sample Code Snippet for making a Bitmap object persistable:
class PersistableBitmap implements Persistable {
int width;
int height;
int[] argbData;
public PersistableBitmap(Bitmap image) {
width = image.getWidth();
height = image.getHeight();
argbData = new int[width * height];
image.getARGB(argbData, 0, width, 0, 0, width, height);
}
public Bitmap getBitmapImage() {
Bitmap image = new Bitmap(width, height);
image.setARGB(argbData, 0, width, 0, 0, width, height);
return image;
}
}

Related

How can I use Open GL ES to crop image?

So I face some problems when I deal with image cropping. I am awared of two possible ways: UIGraphicsBeginImageContextWithOptions combined with drawAtPoint:blendMode: methods and CGImageCreateWithImageInRect. They both work but have some serious disadvantages:the first way takes a lot of time(in my case 7 secs approx.) and memory(I receive memory warning)(suppose I crop an image taken with iPhone camera); the second ends up with rotated image so that you need to put a bunch code to defeat this behavior which I don't want. What I want to know is how, for instance, apple's built in edit function of "Photos" app works, or Aviary or any other photo editor. Consider apple's editor(iOS 8), you can rotate image,change cropping rectangle,they also have blurring(!) outside the cropping rect and so on, but when you apply cropping it takes max 8 mb of memory and it happens immediately. How do they do this?
The only thought I have is that they use the potential of GPU(Aviary, for instance). So,if we combine all this questions in one, how can I use Open GL to make cropping be less painful operation? I've never worked with it, so any tuts,links and sources are welcome.Thank you in advance.
As already mentioned this should most likely not be done with openGL but even if...
The problem most people have is getting the rectangle in which the image should be displayed and the solution looks something like this:
- (CGRect)fillSizeForSource:(CGRect)source target:(CGRect)target
{
if(source.size.width/source.size.height > target.size.width/target.size.height)
{
// keep target height and make the width larger
CGSize newSize = CGSizeMake(target.size.height * (source.size.width/source.size.height), target.size.height);
return CGRectMake((target.size.width-newSize.width)*.5f, .0f, newSize.width, newSize.height);
}
else
{
// keep target width and make the height larger
CGSize newSize = CGSizeMake(target.size.width, target.size.width * (source.size.height/source.size.width));
return CGRectMake(.0f, (target.size.height-newSize.height)*.5f, newSize.width, newSize.height);
}
}
- (CGRect)fitSizeForSource:(CGRect)source target:(CGRect)target
{
if(source.size.width/source.size.height < target.size.width/target.size.height)
{
// keep target height and make the width smaller
CGSize newSize = CGSizeMake(target.size.height * (source.size.width/source.size.height), target.size.height);
return CGRectMake((target.size.width-newSize.width)*.5f, .0f, newSize.width, newSize.height);
}
else
{
// keep target width and make the height smaller
CGSize newSize = CGSizeMake(target.size.width, target.size.width * (source.size.height/source.size.width));
return CGRectMake(.0f, (target.size.height-newSize.height)*.5f, newSize.width, newSize.height);
}
}
I did not test this.
Or since you are on iOS simply create an image view with the desired size, add an image to it and then get the screenshot of the image.

Variable size of CGContext

I'm currently using a UIGraphicsBeginImageContext(resultingImageSize); to create an image.
But when I call this function, I don't know exactly the width of resultingImageSize.
Indeed, I developed some kind of video processing which consume lots of memory, and I cannot process first then draw after: I must draw during the video process.
If I set, for example UIGraphicsBeginImageContext(CGSizeMake(300, 400));, the drawn part over 400 is lost.
So is there a solution to set a variable size of CGContext, or resize a CGContext with very few memory consume?
I found a solution by creating a new larger Context each time it must be resized. Here's the magic function:
void MPResizeContextWithNewSize(CGContextRef *c, CGSize s) {
size_t bitsPerComponents = CGBitmapContextGetBitsPerComponent(*c);
size_t numberOfComponents = CGBitmapContextGetBitsPerPixel(*c) / bitsPerComponents;
CGContextRef newContext = CGBitmapContextCreate(NULL, s.width, s.height, bitsPerComponents, sizeof(UInt8)*s.width*numberOfComponents,
CGBitmapContextGetColorSpace(*c), CGBitmapContextGetBitmapInfo(*c));
// Copying context content
CGImageRef im = CGBitmapContextCreateImage(*c);
CGContextDrawImage(newContext, CGRectMake(0, 0, CGBitmapContextGetWidth(*c), CGBitmapContextGetHeight(*c)), im);
CGImageRelease(im);
CGContextRelease(*c);
*c = newContext;
}
I wonder if it could be optimized, for example with memcpy, as suggested here. I tried but it makes my code crash.

Unable to display image from web on blackberry device

I developed simple code to display an image and title on blackberry screen, but I am not able to display anything (but if I tested for title only it is working).
My requirement is to display image with title in a list.
I have followed this link for fetching image from image url
and here my code in drawlistrow method:
public void drawListRow(ListField list, Graphics g, int index, int y, int w)
{
String text = (String)listElements.elementAt(index);
Bitmap image =GetImage.connectServerForImage("http://toucheradio.com/toneradio/iphone/toriLite/toriLive.png");
g.drawLine(0, y, w, y);
g.drawText(text, 150, y, 60, w);
g.drawBitmap(0,y,image.getWidth(),image.getHeight(),image,0,0);
}
What could be the problem?
And please check the image height and width if its big than your screen resolution, then you have to crop or enlarge the image you are getting. Give some x axis to the drawBitmapMethod i have just giving you the idea like below you can implement.
g.drawBitmap(340, y, image.getWidth(), image.getHeight(), image, 0, 0);

Blackberry - set field background image stetched to fit

How can you scale an image - whether it be using Bitmap or EncodedImage to fill the entire width and height of a field?
Tried this:
protected void paintBackground(Graphics g) {
super.paintBackground(g);
//try bitmap
//g.drawBitmap(0, 0, this.getWidth(), this.getHeight(), image 0, 0);
//try encoded image
g.drawImage(0, 0, this.getContentWidth(), this.getContentHeight(), image, 0, 0, 0);
}
This questn is asked before here is link Blackberry - how to resize image?
In OS 5.0 the api scaleInto method is provided in Bitmap class i.e
http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/system/Bitmap.html#scaleInto%28net.rim.device.api.system.Bitmap,%20int%29
For Below OS 5.0 this bb kb article will be helpful for image manipulations
http://supportforums.blackberry.com/t5/Java-Development/Rotate-and-scale-bitmaps/ta-p/492524

What is BlackBerry's equivalent to Java ME's Image.createImage() from an existing (loaded) image

I have the following Java ME code that I'd like to port to BlackBerry:
Image imgAll = Image.createImage("/fontDigits_200x20.png");
imageDigits = new Image[10];
for(int i = 0; i < imageDigits.length; i++)
imageDigits[i] = Image.createImage(imgAll, i * 20, 0, 20, 20, Sprite.TRANS_NONE);
Basically, it's one image of ten digits that I want to split into 10 individual images and store them into an array. I looked through the docs, but can't find anything similar on EncodedImage or Graphics.
Thank you for any pointers!
UPDATE:
Good news! Apparently there's no way to crop an EncodedImage in such a way as to have a new EncodedImage which is a cropped subset of the original. However, you can do that with a Bitmap, which essentially is the same.
you can use
Bitmap.getARGB(int[] argbData,
int offset,
int scanLength,
int x,
int y,
int width,
int height)
after loading your image
Bitmap imgAll = Bitmap.getBitmapResource("fontDigits_200x20.png");
and off course you can create new Bitmap from this ARGB data.
You can do it directly with the Bitmap.scaleInto function:
Bitmap src;
Bitmap dst = new Bitmap(64,32);
int filterType = Bitmap.FILTER_BILINEAR;
src.scaleInto(srcLeft, srcTop, srcWidth, srcHeight, dst, dstLeft, dstTop, dstWidth, dstHeight, filterType);

Resources