在Java中,有没有办法获得不是.jpeg,.gif,.bmp或.png的文件的BufferedImage?更具体地说,我希望能够创建一个BufferedImage .jpg。我知道的唯一方法是ImageIO.read,如果文件不是任何这些类型,它将返回null。有没有其他方法可以制作BufferedImage? 目前我的代码如下所示:
private void sendImage(BufferedImage image){
try{
if(output == null){
showMessage("MAKE SURE THAT YOU ARE CONNECTED TO SOMEONE!\n");
}
else{
if(image == null)System.out.println("image is null in sendImage");
output.writeObject("CODE - 4");
output.flush();
System.out.println("here");
output.writeObject(image);
System.out.println("done");
output.flush();
System.out.println("Just sent code " + 4);
}
}catch(IOException ioException){
System.out.println("\nERROR! UNABLE TO SEND IMAGE CODE!");
ioException.printStackTrace();
}
堆栈跟踪如下所示:
java.io.NotSerializableException: java.awt.image.BufferedImage
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at server.Server.sendImage(Server.java:432)
at server.Server.access$300(Server.java:32)
at server.Server$2.actionPerformed(Server.java:95)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
是的,程序输出“here”然后与上面的堆栈跟踪崩溃。如果没有以不同的格式获得它,是否有替代品可以来自任何格式的图像(对于超级奖励点,也可以是可序列化的)。
答案 0 :(得分:1)
Java中最常见的图像读者/编写者是:
使用imageio和JAI,您可以读取/写入jpg图像。
答案 1 :(得分:0)
我认为您不希望将图像直接写入流中。你想要的是ImageIO.write,像这样:
<string name="google_app_id">YOUR_PROJECT_NUMBER</string>
上面的代码会将您的缓冲图像写为png。如果你想要一个jpeg,这应该有效:
import javax.imageio.ImageIO;
.
.
.
.
ImageIO.write(image, "png", output);