Results 1 to 5 of 5
  1. #1
    mikemil is offline Member
    Join Date
    Aug 2012
    Location
    Flower Mound, TX
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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 07:35 PM.

  3. #3
    mikemil is offline Member
    Join Date
    Aug 2012
    Location
    Flower Mound, TX
    Posts
    3
    Rep Power
    0

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

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

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

  5. #5
    mikemil is offline Member
    Join Date
    Aug 2012
    Location
    Flower Mound, TX
    Posts
    3
    Rep Power
    0

    Default 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

  1. Image Size in PDF
    By Shabber102 in forum Advanced Java
    Replies: 1
    Last Post: 02-07-2012, 04:37 PM
  2. Replies: 1
    Last Post: 07-28-2011, 09:48 AM
  3. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM
  4. Image size in a JFrame
    By nickbeacroft in forum AWT / Swing
    Replies: 2
    Last Post: 06-26-2008, 05:08 PM
  5. how to set an image size
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •