0

i develop a java swing application using eclipse. here i try to create a page which has a watermark on some of the page screen.

i use JLabel as a container to have the image watermark

in Pages :

BufferedImage bi = ImageIO.read( "photoFile" ) ;
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
ImageIO.write( bi , "jpg" , bos ) ;
byte[] data = bos.toByteArray () ;
data = getBackground( "text" , data ) ;

JLabel background = new JLabel ( new ImageIcon ( data ) ) ;
background.setPreferredSize( new Dimension ( 1700 , 3500 ) ) ;
background.add( components )

getBackground contains below code :

public static byte[] getBackground(String text, byte[] imageByte) throws IOException {
        // initializes necessary graphic properties
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        BufferedImage image = ImageIO.read(bis);
        Graphics2D w = (Graphics2D) image.getGraphics();
        w.drawImage(image, 0, 0, null);
        AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f);
        w.setComposite(alphaChannel);
        w.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        w.setColor(Color.decode( "#FFFFFF" ) ) ;

        w.setFont(new Font(Font.ARIAL, Font.ITALIC , 14));

        FontMetrics fontMetrics = w.getFontMetrics();
        text = text ;
        Rectangle2D rect = fontMetrics.getStringBounds(text, w);


        w.translate( image.getWidth() / 2.0f , image.getHeight() / 2.0f ) ;

        AffineTransform at = new AffineTransform() ;
        double opad = image.getHeight() / ( double ) image.getWidth() ;
        double angle = Math.toDegrees( Math.atan( opad) ) ;
        double idegrees = -1 * angle ;
        double theta = ( 2 * Math.PI * idegrees ) / 360 ;
        at.rotate( theta ) ;
        w.transform( at ) ;

        float x1 = ( int ) rect.getWidth() / 2.0f * -1 ;
        float y1 = ( int ) rect.getHeight() / 2.0f ;
        w.translate( x1 , y1 ) ;


        for ( int i = image.getHeight() * -1 ; i < image.getHeight() ; i += 40 ) {
            w.drawString( text , 0.0f , i ) ;
        }

        w.dispose(); 

        //*return
        ByteArrayOutputStream baos = new ByteArrayOutputStream();            
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);               
        encoder.encode(image);  
        return baos.toByteArray();
    }

when i open the first page it success, but when i open another page with the same process calling for watermark, it throws me an error saying "java.lang.OutOfMemoryError: Java heap space"

and when i check the line, it says error in line where "encoder.encode(image);" exists.

can someone tell me what should i do or what is wrong with my process?

Thanks in advance..

rondi
  • 71
  • 11
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Apr 03 '20 at 08:52
  • 1
    One of many reasons we require an MRE / SSCCE is that often the person asking the question cannot accurately identify the code causing the problem. In [this case](https://i.stack.imgur.com/1cYu3.png), we can be confident it is not in the code seen above. – Andrew Thompson Apr 03 '20 at 09:08
  • I have no idea why you are converting the BufferedImage to a byte array and then back to a BufferedImage. Just paint directly onto the BufferedImage. Then just return the BufferedImage. I don't see any reason to be playing with the byte array streams. – camickr Apr 03 '20 at 15:51

0 Answers0