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
JamesColeman
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified
Accepted Solution

Convert Image to JPEGEncodedImage

Is there a way to convert an Image to JPEGEncodedImage?

 

Thanks!

Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.997 Dev OS, Z30 (STA100-5)-10.3.1.997 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Convert Image to JPEGEncodedImage

Image as in javax.microedition.lcdui.Image, net.rim.device.api.ui.image.Image, or just general "image"?

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
JamesColeman
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified

Re: Convert SVGImage to JPEGEncodedImage

Good question. I made an error, It is actually "javax.microedition.m2g.SVGImage" and I need to get it to a JPEGEncodedImage. 

 

So far sewing together multiple screen shots is the only thing I have come up with.

 

Thanks for the help!

Developer
ydaraishy
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Convert SVGImage to JPEGEncodedImage

Not going to be easy -- as you know, SVG is an unrasterized vector description, JPEG is obviously not this; screenshotting like you say, will work. Any other method will require similar methods: rasterize, then dump the image (not sure how else to go about it).

Developer
JamesColeman
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified

Re: Convert SVGImage to JPEGEncodedImage

Yep vector to bitmap conversion.

 

Oh well, I already have the screenshot method working for SVGimages of a set size, I just have to make it work for any size SVGimage.  I was hoping to avoid having the user see me tile the screen shots, but I'll just go ahead and do it that way, at least it works and isn't too difficult.

 

Thanks for taking the time to reply!

Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.997 Dev OS, Z30 (STA100-5)-10.3.1.997 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Convert SVGImage to JPEGEncodedImage

I think I figured it out but havn't tried it.

 

Create a Bitmap, then use it to create a Graphics object.

 

Take a look at the SVG folder in the JDE's samples. In it is the svgcldcdemo. Basically rewrite it so you get your SVGImage, then instead of having paint called you do it manually. This will draw the SVGImage to the Graphics and thus the Bitmap.

 

Then call JPEGEncodedImage.encode and pass the Bitmap. Walla, a SVGImage to a JPEGEncodedImage.

 

Hope that helps.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
JamesColeman
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified

Re: Convert SVGImage to JPEGEncodedImage

Wow, I think I know what you mean.  Totally Brilliant!

 

I looked at that code awhile ago and for some reason it never occurred to me.  I should be able to

 

        // Bind target Graphics.
        _scalableGraphics.bindTarget(graphics);  
       
        // Render the svg image/ model.       
        _scalableGraphics.render(0, 0, SVGimage);       

        // Release bindings on Graphics.
        _scalableGraphics.releaseTarget();

 

I'll start coding after I get a bite to eat.

Developer
JamesColeman
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified

Re: Convert SVGImage to JPEGEncodedImage

Bummer. So close, and yet so far.  I'm getting a "Bitmap is too large for graphics surface" error on the following:

 

SaveFileGraphics = Graphics.create(SaveFileBitmap);

 

 

Bitmaps blow up somewhere between 1024 x 768 and 1600 x 1200, and I need 2048 x 1536.    I just sent a email to RIM to see if what they can do about it for future releases.

 

Thanks Again for the help!

 

Developer
rcmaniac25
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.997 Dev OS, Z30 (STA100-5)-10.3.1.997 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Convert SVGImage to JPEGEncodedImage

I remember reading somewhere on these forums that someone tried rendering a SVGImage to a Bitmap (I couldn't find that post so I created what I thought would work) and I remember that he ran into memory issues as well. If you can find that post or one that has to do with running out of memory with very large Bitmaps then maybe you can resolve that.

 

On a workaround, maybe you could render a smaller portion of the image to make sure it works, then piece together the smaller portions together. Also instead of sending RIM an email, put it in the Issue Tracker (there might be an entry there already, I havn't checked). That way others can vote if they want that fixed and it can get resolved. Just be sure to make the entry in full detail, link to this post, etc. that way they can better reproduce the error.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
JamesColeman
Posts: 266
Registered: ‎04-24-2009
My Device: Not Specified

Re: Convert SVGImage to JPEGEncodedImage

It is time to give back to a forum that has been extremely helpful to meHopefully other developers can make use of the following code.

 

It required tileing images so this was slightly challenging, but here is some code in case any one else wants to go from scalable graphics or SVGImages to bitmap graphics.

 

It is time to give back to a forum that has been extremely helpful to meHopefully other developers can make use of the following code.

 

It required tileing images so this was slightly challenging, but here is some code in case any one else wants to go from scalable graphics or SVGImages to bitmap graphics.

 

 public JPEGEncodedImage GetJPGImage(){
        ScalableGraphics TempScalableGraphics;
        SVGRect rect;
        Bitmap bm;
        Graphics TempGraphics;
        Bitmap SaveFileBitmap;
        
        
        int Columns = ImageWidth/ViewportWidth;
        int Rows = ImageHeight/ViewportHeight;
        int LastColumn = ImageWidth - (Columns * ViewportWidth);
        int LastRow = ImageHeight - (Rows * ViewportHeight);
        
        bm = new Bitmap(ViewportWidth, ViewportHeight);
        SaveFileBitmap = new Bitmap(ImageWidth, ImageHeight);
        TempGraphics = Graphics.create(bm);
        TempScalableGraphics = ScalableGraphics.createInstance();

       //I had to make sure that the scale was at 1 and PanPoint was at 0,0.  You may not have to.
        _svg.setCurrentScale(1);
        PanPoint = _svg.getCurrentTranslate();
        SVGPoint TempPanPoint = PanPoint;
        PanPoint.setX(0);
        PanPoint.setY(0);
        
        
        rect = _svg.createSVGRect();
        rect.setHeight(ViewportHeight);
        rect.setWidth(ViewportWidth);
        
                
        for(int i=0; i<Rows+1; i++){
            for(int j=0; j<Columns+1; j++){
                rect.setX(j * ViewportWidth);
                rect.setY(i * ViewportHeight);
                _svg.setRectTrait("viewBox", rect);
                TempScalableGraphics.bindTarget(TempGraphics);  
                TempScalableGraphics.render(0, 0, _image);    
                TempScalableGraphics.releaseTarget();
                if (i < Rows  && j < Columns){
                    bm.scaleInto(0, 0, ViewportWidth, ViewportHeight, SaveFileBitmap, (j * ViewportWidth), (i * ViewportHeight), ViewportWidth, ViewportHeight, Bitmap.FILTER_LANCZOS);
                }
                else if (i < Rows && j == Columns){
                    bm.scaleInto(0, 0, LastColumn, ViewportHeight, SaveFileBitmap, (j * ViewportWidth), (i * ViewportHeight), LastColumn, ViewportHeight, Bitmap.FILTER_LANCZOS);
                }
                else if (i == Rows && j < Columns){
                    bm.scaleInto(0, 0, ViewportWidth, LastRow, SaveFileBitmap, (j * ViewportWidth), (i * ViewportHeight), ViewportWidth, LastRow, Bitmap.FILTER_LANCZOS);
                }

                else {
                    bm.scaleInto(0, 0, LastColumn, LastRow, SaveFileBitmap, (j * ViewportWidth), (i * ViewportHeight), LastColumn, LastRow, Bitmap.FILTER_LANCZOS);
                }
            }
        }
        
        //Here I reset my viewbox, you may noit have to
        rect.setX(0f);
        rect.setY(0f);
        _svg.setRectTrait("viewBox", rect);

        //Some graphics variables can get very large so I make sure they are emptied
        TempScalableGraphics = null;
        TempGraphics = null;
        rect = null;
        bm = null;
        
        return JPEGEncodedImage.encode(SaveFileBitmap, 80);
  }