Results 1 to 5 of 5
- 08-08-2012, 06:22 PM #1
Member
- Join Date
- Aug 2012
- Location
- Flower Mound, TX
- Posts
- 3
- Rep Power
- 0
How do I determine image size ahead of time when image has variable lines of text?
I work on a POS project and we are going to integrate with a vendor for sending electronic receipts. They require that we send them a jpg image of our receipt, which they then email to the customer.
The receipt will contain a company logo at the top, then a variable number of lines of text, which should all be the same width and then a barcode at the bottom.
I started playing around with the ImageIO and Graphics classes and can generate a jpg with my text lines included but my concern is that I don't know ahead of time how many lines of text will be in the receipt. The customer could buy 1 item or 900 items!
How do I correctly pick an image size for my BufferedImage class?
My code creates a BufferedImage with height/width of 300/300 and then goes into a loop filling and writing TextLayouts, but don't I run a chance of eventually writing more text/content than can fit in my image size? I started with the sample from the Java 2D tutorial on drawing mutliple lines of text
-
Re: How do I determine image size ahead of time when image has variable lines of text
I am not a professional coder, nowhere near, and so please take my recommendations with a grain of salt.
To try to answer the question directly, if you output your receipt to a Swing text component such as a JTextArea, JEditorPane or JTextPane, you can then create an image of the correct size by creating a BufferedImage the same size as the completed component and then drawing to it by "painting" the component into the BufferedImage.
But having said that, you seem to be creating a report, a process that seems to be best done using a report-generating library -- are you using this to help create your receipts? I've never used one, but I do know that many of them can create PDF files which seems to me would serve your purposes quite well.Last edited by Fubarable; 08-08-2012 at 06:35 PM.
- 08-08-2012, 06:50 PM #3
Member
- Join Date
- Aug 2012
- Location
- Flower Mound, TX
- Posts
- 3
- Rep Power
- 0
Re: How do I determine image size ahead of time when image has variable lines of text
Thanks, guess I should have stated it more directly. The image MUST be jpg. Otherwise, if they took PDF, I would be using iText and done with the entire issue. And apparently iText "doesn't do JPG!".
If I can generate the image myself, I would rather do that. Otherwise, maybe an option is to generate a PDF and find something that will let me "save as jpg".
- 08-08-2012, 07:22 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: How do I determine image size ahead of time when image has variable lines of text
If you know the sizes of images and fonts you are drawing, the calculate the height based upon these values. If you do not (for instance, fonts might change based upon text or platform), then write the text once to a dummy image of the size of your choosing. Keep a variable that denotes how far down you write - this indicates the height of the image you need to create (add some padding for the bottom). Then, create a new image with the calculated height, and redraw to this image.
- 08-09-2012, 06:18 PM #5
Member
- Join Date
- Aug 2012
- Location
- Flower Mound, TX
- Posts
- 3
- Rep Power
- 0
Re: How do I determine image size ahead of time when image has variable lines of text
I think that's going to work for me. A bit more work than I expected, but I am going to iterate thru all the 'components' that I need to merge into the image and calculate the height. I don't even have to create a dummy image.
Thanks for the help!
Similar Threads
-
Image Size in PDF
By Shabber102 in forum Advanced JavaReplies: 1Last Post: 02-07-2012, 03:37 PM -
How to import image, draw circles/text on it, and save a new image to disk
By InTheEndo in forum Java 2DReplies: 1Last Post: 07-28-2011, 08:48 AM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM -
Image size in a JFrame
By nickbeacroft in forum AWT / SwingReplies: 2Last Post: 06-26-2008, 04:08 PM -
how to set an image size
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks