12-03-2009 05:02 PM
Is there a way to convert an Image to JPEGEncodedImage?
Thanks!
Solved! Go to Solution.
12-03-2009 05:35 PM
Image as in javax.microedition.lcdui.Image, net.rim.device.api.ui.image.Image, or just general "image"?
12-03-2009 05:45 PM
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!
12-03-2009 06:08 PM
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).
12-03-2009 06:27 PM
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!
12-03-2009 06:29 PM
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.
12-03-2009 06:48 PM
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.
12-03-2009 07:59 PM
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!
12-03-2009 09:37 PM
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.
02-27-2010 06:20 PM
It is time to give back to a forum that has been extremely helpful to me. Hopefully 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 me. Hopefully 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);
}