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