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: 46
Registered: ‎12-01-2009
My Device: Not Specified

From Graphics to Photos

There are 2 parts to this question:

 

1. Can I take a BlackBerry Graphics object and convert it into PNG image data?

 

If not directly, then perhaps Graphics -> Bitmap -> PNG? I know Bitmap -> PNG is possible, but can't find anything on Graphics -> Bitmap.

 

If PNG is not an option, JPEG would be fine too.

 

2. Using some filesystem API, is it possible, given PNG image bytes and a file name, to write the PNG bytes to the Pictures/Photos folder that can then be accessed using the built-in photo/image viewer?

 

 

Any answers/advice for either of the steps is appreciated.

 

Thanks =)

 

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: From Graphics to Photos

This questions exists for nearly every platform that ever existed. The short answer is no unless you create the Graphics object from an image.

 

If someone has figured out how to do this I will defiantly give Kudos because, for whatever reason, almost every other app I work on requires this. I always need to create a buffer to draw to then draw the buffer to the final destination Graphics object.

---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
Posts: 46
Registered: ‎12-01-2009
My Device: Not Specified

Re: From Graphics to Photos

[ Edited ]

I've found this thread:

 

http://supportforums.blackberry.com/t5/Java-Development/Bitmap-to-ByteArray/m-p/21892#M736

 

there is a post that answers #2 -- saving to the Photos folder.

 

What remains is going from a Graphics object to a byte array of any image format readable by the built-in image viewer.

 

Edited:

 

Graphics -> Bitmap might be a step in the right direction.

 

I've tried this:

 

 

		Bitmap bitmap = new Bitmap(1000, 1000);
Graphics graphics = new Graphics(bitmap);
graphics.setColor(Color.BLACK);
graphics.drawRect(5, 5, 990, 990);

PNGEncodedImage pngEncImg = PNGEncodedImage.encode(bitmap);
System.out.println(pngEncImg.getData());

 

 

However, Line 2 throws an IllegalArgumentException -- because it's read-only? Any way to make it writeable?

 

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: From Graphics to Photos

The documentation says to use Graphics.create instead of new Graphics.

---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
Posts: 46
Registered: ‎12-01-2009
My Device: Not Specified

Re: From Graphics to Photos

new Graphics(Bitmap) is deprecated at least starting 4.7, but I'm on 4.6 -- .create does not exist in 4.6.

 

I almost have this working exactly as I need as I'm able to EDIT an image and save it, but not create one using new Bitmap(width, height). In the latter case, IllegalArgumentException is thrown. Any idea why?

 

As a workaround, of course, I can create a blank image with the correct dimensions and edit that every time, but surely there must be another way.

 

Highlighted
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: From Graphics to Photos

I learn something new every day, I'm so used to Graphics objects (on BlackBerry and other platforms) having a static creation function I assumed it was around earlier then 4.7.

 

As for your Exception, your getting that when you create a new Bitmap? That's strange, have you tried using a different values? I never encountered that exception (unless I had a negative size). But if the code you posted is the exact same code that you are testing with then I see no reason for the exception to be thrown.

---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.