Menu

java.lang.OutOfMemoryError

Help
V H
2006-12-05
2013-03-22
  • V H

    V H - 2006-12-05

    I got an out of memory exception when trying an image.  Here's the exception:

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
            at java.util.Arrays.copyOf(Arrays.java:2760)
            at java.util.Arrays.copyOf(Arrays.java:2734)
            at java.util.Vector.ensureCapacityHelper(Vector.java:226)
            at java.util.Vector.add(Vector.java:728)
            at uk.org.JBarcode_Recognition.Barcodes.Barcode_CODE_128.<init>(Barcode_CODE_128.java:67)
            at uk.org.JBarcode_Recognition.Barcodes.Barcode_CODE_128_CONDENSED.<init>(Barcode_CODE_128_CONDENSED.java:47)
            at uk.org.JBarcode_Recognition.Barcodes.Barcode._checkLine(Barcode.java:584)
            at uk.org.JBarcode_Recognition.Barcodes.Barcode._combineAndCheck(Barcode.java:432)
            at uk.org.JBarcode_Recognition.Barcodes.Barcode._combineAndCheck(Barcode.java:645)
            at uk.org.JBarcode_Recognition.Barcodes.Barcode.getBarcode(Barcode.java:82)
            at uk.org.JBarcode_Recognition.JBarcode_Recognition.GetBarcode(JBarcode_Recognition.java:75)
            at uk.org.JBarcode_Recognition.JBarcode_Recognition.GetBarcode(JBarcode_Recognition.java:199)
            at barcode.Test._getSheet(Test.java:106)
            at barcode.Test.main(Test.java:90)

    Please let me have an email to send in the test image.

    Below is the code:
    [code]
    /*
    * Test.java
    *
    * Created on December 5, 2006, 5:02 PM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package barcode;

    /**
    *
    * @author vho
    */

    /*
    *
    *  JBarcode_Recognition
    *
    *  Copyright 2004 Andi McLean
    *
    *  This program is free software; you can redistribute it and/or
    *  modify it under the terms of the GNU General Public License
    *  as published by the Free Software Foundation; either version 2 of
    *  the License, or (at your option) any later version.
    *
    *  This program is distributed in the hope that it will be useful,
    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    *  License document supplied with your distribution for more details.
    *
    */

    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;

    import javax.imageio.ImageIO;

    import uk.org.JBarcode_Recognition.JBarcode_Recognition;
    import uk.org.JBarcode_Recognition.Barcodes.Barcode;

    /**
    * @author Jim Tupper
    * @author Andi Mclean
    *
    */
    public class Test {
       
        private BufferedImage _image = null;
       
        public BufferedImage getImage() {
            return _image;
        }
       
        public Test() {
            //Load a test image:
            try {
                String fileName = "test.gif";   //@todo: change the file here
               
                Toolkit toolKit = java.awt.Toolkit.getDefaultToolkit();
                Image image = ImageIO.read(new File(fileName));
               
                _image = new BufferedImage(image.getWidth(null), image.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);
                Graphics2D big = _image.createGraphics();
                big.drawImage(image, 0, 0, null);
                big.dispose();
               
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
       
        public static void main(String[] args) {
            System.out.println("Scanning a page");
            Test barcodeScan = new Test();
           
            System.out.println("Processing Barcode");
            Barcode barcode = null;
           
            barcode = _getSheet(barcodeScan);
           
            System.out.println(barcode);
           
            barcode = _getSheetFull_128(barcodeScan);
           
            System.out.println(barcode);
           
            barcode = _getSheetEan_8(barcodeScan);
           
            System.out.println(barcode);
           
        }
       
        private static Barcode _getSheet(Test barcodeScan) {
            Barcode barcode;
            barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                barcodeScan.getImage().getHeight()), 0.7,
                JBarcode_Recognition.HORIZONTAL,
                JBarcode_Recognition.NO_COLOUR_INVERT,
                JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_CODE_128_CONDENSED_DIGITS);
            return barcode;
        }
       
        private static Barcode _getSheetFull_128(Test barcodeScan) {
            Barcode barcode;
            barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                new Rectangle(230, 550, 960, 260), 0.7,
                JBarcode_Recognition.HORIZONTAL,
                JBarcode_Recognition.NO_COLOUR_INVERT,
                JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_AUTO);
            return barcode;
        }
       
        private static Barcode _getSheetEan_8(Test barcodeScan) {
            Barcode barcode;
            barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                barcodeScan.getImage().getHeight()), 0.7,
                JBarcode_Recognition.HORIZONTAL,
                JBarcode_Recognition.NO_COLOUR_INVERT,
                JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_AUTO);
            return barcode;
        }
       
        private static Barcode _getScannerBook_3of9(Test barcodeScan) {
            Barcode barcode;
            barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                barcodeScan.getImage().getHeight()), 0.5,
                JBarcode_Recognition.HORIZONTAL,
                JBarcode_Recognition.NO_COLOUR_INVERT,
                JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_3_OF_9);
            return barcode;
        }
       
        /**
         * @param barcodeScan
         * @return
         * Not working
         */
        private static Barcode _getScannerBook_EAN13(Test barcodeScan) {
            Barcode barcode;
            barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                barcodeScan.getImage().getHeight()), 0.25,
                JBarcode_Recognition.HORIZONTAL,
                JBarcode_Recognition.NO_COLOUR_INVERT,
                JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_AUTO);
            return barcode;
        }
        /**
         * @param barcodeScan
         * @return
         */
        private static Barcode _getScannerBook_UPCA_5DIGIT(Test barcodeScan) {
            Barcode barcode;
            barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                barcodeScan.getImage().getHeight()), 0.25,
                JBarcode_Recognition.HORIZONTAL,
                JBarcode_Recognition.NO_COLOUR_INVERT,
                JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_UPC_A);
            return barcode;
        }
       
        /**
         * @param barcodeScan
         * @return
         */
        private static Barcode _getScannerBook_Ext5(Test barcodeScan) {
            Barcode barcode;
            barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                barcodeScan.getImage().getHeight()), 0.25,
                JBarcode_Recognition.HORIZONTAL,
                JBarcode_Recognition.NO_COLOUR_INVERT,
                JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_UPC_EXTENSION_5);
            return barcode;
        }
       
        /**
         * @param barcodeScan
         * @return
         */
        private static Barcode _getOReillyBarcode(Test barcodeScan) {
            Barcode barcode;
            barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                barcodeScan.getImage().getHeight()), 0.5,
                JBarcode_Recognition.HORIZONTAL,
                JBarcode_Recognition.NO_COLOUR_INVERT,
                JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_AUTO);
           
            if (!barcode.isValid()) {
                barcode = JBarcode_Recognition
                    .GetBarcode(barcodeScan.getImage(),
                    new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                    barcodeScan.getImage().getHeight()),
                    0.5, JBarcode_Recognition.HORIZONTAL,
                    JBarcode_Recognition.NO_COLOUR_INVERT,
                    JBarcode_Recognition.TRY_DIRECTION_REVERSE,
                    Barcode.BT_AUTO);
            }
           
            if (!barcode.isValid()) {
                barcode = JBarcode_Recognition.GetBarcode(barcodeScan.getImage(),
                    new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                    barcodeScan.getImage().getHeight()), 0.5,
                    JBarcode_Recognition.VERTICAL,
                    JBarcode_Recognition.NO_COLOUR_INVERT,
                    JBarcode_Recognition.NO_DIRECTION_REVERSE, Barcode.BT_AUTO);
            }
            if (!barcode.isValid()) {
                barcode = JBarcode_Recognition
                    .GetBarcode(barcodeScan.getImage(),
                    new Rectangle(0, 0, barcodeScan.getImage().getWidth(),
                    barcodeScan.getImage().getHeight()),
                    0.5, JBarcode_Recognition.VERTICAL,
                    JBarcode_Recognition.NO_COLOUR_INVERT,
                    JBarcode_Recognition.TRY_DIRECTION_REVERSE,
                    Barcode.BT_AUTO);
            }
            //      new Rectangle( 270,2930,400,250 ), Refill pad barcode
           
            //      new Rectangle( 170,1710,420,250), //Pocket O'Reilly
            //        new Rectangle( 665,1665, 350, 40 ), Nice day barcode
            //        new Rectangle( 2010,90, 80, 2000 ), // Verticle (So-this-is-how-you)
            //      new Rectangle( 300,380, 600, 40 ), // *XNO-ACTION*
            //      Barcode barcode = GetBarcode( barcodeScan.getImage(), new
            // BarcodeRectangle( 300, 350, 840, 350, 40 ), 0.5 );
            //      Barcode barcode = GetBarcode( barcodeScan.getImage(), new
            // BarcodeRectangle( 1000, 380, 1600, 380, 40 ), 0.5 );
            //      Barcode barcode = GetBarcode( barcodeScan.getImage(), new
            // BarcodeRectangle( 1670, 380, 2300, 380, 40 ), 0.5 );
           
            return barcode;
        }
       
    }
    [/code]

     
    • Andi McLean

      Andi McLean - 2006-12-06

      when calling your program you will need to tell the java vm to use extra memory.

      -Xms256m -Xmx256m

      This will tell it to use 256 megs of memory, if that still isn't enough then change the 256 for a bigger number eg

      -Xms1024m -Xmx1024m

      cheers
      Andi

       
    • V H

      V H - 2006-12-06

      The change does NOT make any difference.  There are two problems with this:

      1) It takes a long time before out of memory.  Obvious performance problem, or infinite loop here.  The later is likely the problem.

      2) For a small barcode, there is no reason to take that long or that much of memory.  This is a must be fixed problem.

      If I can submit an image, anyone can play with and track down the problem.

       
    • V H

      V H - 2006-12-06

      The infinite loop is in this do while loop:
      [code]
          public Barcode_CODE_128(boolean[] image, boolean invert)
          {
              super(image, invert, NUMSTATS, NUMBARS);
             
              Vector bar = new Vector();
              int offset = 0;
              char value;
              do
              {
                  value = _decode(_getCharacter(offset, NUMBARS));
                  offset += NUMBARS;
                 
                  bar.add(new Integer(value));
                 
                 // System.out.print("|" + (int) value);
              } while (value != 255 && value != STOP);
             
              if (_checkDigit(bar))
              {
                  _valid = true;
                  _text = _makeText(bar);
              }
             
              //System.out.println();
          }

      [/code]

      I think I may hit something here.  First, the image is probably not barcode 128.  So, someone may say, just fix it.  However, there are two problem with this:

      1) The library should guess the type of barcode for me.
      This is a nice to have.
      2) The library must work with bad image, invalid image (identify and reject them).
      The reason is that if I have a server, and people submit a bad image, that would blow away my server.  Not very desirable.

      I also suggest not to use boolean [][] as an image, but use integer, or byte.  The reason is that later, you can intelligently guess the value if the image is not in its perfect quality.  You can also send back the percent of confidence to the calling routine.

       

Log in to post a comment.

MongoDB Logo MongoDB