项目应用服务器tomcat7,在开发(windows)、测试环境(linux 64bit)均正常。在生产环境(linux 64bit)一直启动不起来,也没有报错。
最终定位问题:执行到bufferedimage.createGraphics();便停止不动了。最终在启动脚本增加
JAVA_OPTS="-Djava.awt.headless=true"解决问题。
---------------------------------------------------------------
问题解决了,但是至于为什么造成这种问题,还是不是很明白。
参照了http://www.oschina.net/translate/using-headless-mode-in-java-se的文章,依旧是一头雾水。
按照上述文章的介绍,写了一个测试类:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); boolean headless_check = ge.isHeadless(); System.out.println("================java.awt.headless:"+headless_check);
输出结果:在测试环境(linux),抛出异常:
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable. at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method) at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65) at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:110) at java.security.AccessController.doPrivileged(Native Method) at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:191) at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:102) at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81) at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1182) at com.tydic.BufferedImageCreateGraphicBug.main(BufferedImageCreateGraphicBug.java:23)
但是在生产环境(linux),打印结果
================java.awt.headless:false
,写一个java类在生产环境(linux)上执行如下代码:
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.setBackground(Color.RED); g2.clearRect(0, 0, width, height); System.getProperty("user.dir"); File f = new File(filename); try { ImageIO.write(bi, formatName , f); } catch (IOException e) { e.printStackTrace(); }
可以正常生成图片!
但是在web应用中,却无法生成图片,且没有抛出异常,一直卡住不动,不知为什么.
相关推荐
BufferedImage bufImg = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = bufImg.createGraphics(); g2d.drawImage(srcImg, 0, 0, 20, 20, null); cb.setIcon(new ImageIcon...
Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bufferedImage; } private static int getBufferedImageType(Image image) { // 根据源Image的色彩...
BufferedImage image = new BufferedImage(400, 120, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2d = image.createGraphics(); g2d.setColor(Color.BLUE); g2d.fillRoundRect(0, 0, 400, 120, 40, 40); ...
image=new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB); ggg=image.createGraphics(); Rectangle2D rect=new Rectangle2D.Double(0,0,200,200); ggg.setColor(getBackground()); ggg.fill(rect); ...
java给图片加水印Demo,主要用到的函数:Graphics2D g2d = bufferedImage.createGraphics(); g2d.drawImage、g2d.setColor、g2d.setFont、g2d.drawString
BufferedImage image = new BufferedImage(400, 120, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2d = image.createGraphics(); g2d.setColor(Color.BLUE); g2d.fillRoundRect(0, 0, 400, 120, 40, 40); ...
BufferedImage image = new BufferedImage(400, 120, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2d = image.createGraphics(); g2d.setColor(Color.BLUE); //g2d.fillRoundRect(0, 0, 400, 120, 40, 40);...
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); //基于图片对象打开绘图 Graphics2D graphics = image.createGraphics(); //绘制圆形 graphics.setColor...
BufferedImage bufImg = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = bufImg.createGraphics(); FontMetrics fm = g2d.getFontMetrics(font); int strWidth = fm....
判断图像通道顺序可以通过BufferedImage的getType()方法来实现,该方法返回图像的类型,例如BufferedImage.TYPE_3BYTE_BGR表示图像为BGR格式,BufferedImage.TYPE_INT_RGB表示图像为RGB格式。 在判断图像通道顺序时...
Graphics2D graphics2D = bufferedImage.createGraphics(); barcode.paint(graphics2D); JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(output); JPEGEncodeParam jpegEncodeParam = ...
BufferedImage mergedImage = new BufferedImage(totalWidth, maxHeight, BufferedImage.TYPE_INT_ARGB); // 绘制图片 Graphics2D g2d = mergedImage.createGraphics(); int x = 0; for (String path : image...
你可以创建一个带有Alpha通道的BufferedImage对象,类型为` BufferedImage.TYPE_INT_ARGB` 或 `BufferedImage.TYPE_INT_ARGB_PRE`。前者适用于大多数情况,而后者对于预乘Alpha的图像更合适。 ```java ...
这是因为分配给JVM的堆内存太小,不足以支持当前应用的需求。可以通过增加`-Xmx`参数来设置最大堆内存大小。 #### 四、内存泄漏识别与定位 1. **工具选择**:可以使用如VisualVM、JProfiler等工具来监控和诊断内存...
BufferedImage mask = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY); Graphics2D g2d = mask.createGraphics(); g2d.setColor(Color.BLACK); g2d....
BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = scaledImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, ...
Graphics2D g2d = bufferedImage.createGraphics(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); g2d.drawImage(bufferedImage, 0, 0, this); g2d.dispose(); } // 其他...
BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); // createGraphics Graphics2D g = bi.createGraphics(); // set background g.setBackground(Color.WHITE); g....
另一个方法可能是`compositeImages`,它接受两个BufferedImage对象,一个作为背景,一个作为前景,返回一个新的BufferedImage,其中前景图被合成到背景图上。 综上所述,这个资源包提供了基于Java的二维码生成、...
Graphics2D g2d = bufferedImage.createGraphics(); g2d.drawImage(image, 0, 0, null); g2d.dispose();` 5. 使用BufferedImage:现在你可以使用BufferedImage进行任意图像处理操作,例如绘制到画布上、保存为其他...