java Java将字节[]转换为图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11247595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java converting byte[] to image
提问by mingle
I know this seems like a common question but i have looked all over the internets, and tried many different tutorials and methods for doing this. I think i am close, but not sure. Also i am using Play Framework but it should be the same for java. here is my error
我知道这似乎是一个常见问题,但我已经浏览了整个互联网,并尝试了许多不同的教程和方法来做到这一点。我想我很接近,但不确定。我也在使用 Play Framework,但它应该与 java 相同。这是我的错误
javax.image.IIOException: I/O error reading PNG header!
at com.sun.plugins.png.PNGImageReader.readHeader(Unknown Source)
...
...
Caused by: java.io.EOFException
at javax.imageio.stream.ImageInputStreamImpl.readFully(Unknown Source)
...
Here is my code where i get the picture among other things from a form and convert the image to a byte[] and store in MS SQL db.
这是我的代码,我从表单中获取图片,并将图像转换为字节 [] 并存储在 MS SQL 数据库中。
@Transactional
public static Result submitTrailer(){
filledForm = newTrailerForm.bindFromRequest();
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = body.getFile("file");
String fileName = picture.getFilename();
System.out.println(fileName);
String contentType = picture.getContentType();
System.out.println(contentType);
final File file = picture.getFile();
filledForm.get().setContentType(contentType);
try{
BufferedImage originalImage = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, contentType, baos);
filledForm.get().setImage(baos.toByteArray());
baos.flush();
baos.close();
filledForm.get().save();
}catch(IOException e){
e.printStackTrace();
}
return ok(views.html.index.index.render());
}
here is where i am trying to covert the byte[] back to an image so i can display it in html
这是我试图将字节 [] 转换回图像的地方,以便我可以在 html 中显示它
public File getConvertedPicture(){
File imageFile;
System.out.println("byteToImage() called");
if(getImage()==null){
System.out.println("getByteImage()==null");
return null;
}else{
try{
ByteArrayInputStream bis = new ByteArrayInputStream(getImage());
imageFile=File.createTempFile("pattern", ".suffix");
Iterator<?> readers = ImageIO.getImageReadersByFormatName("PNG");
ImageReader reader = (ImageReader) readers.next();
Object source = bis;
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Image image = reader.read(0, param);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);
ImageIO.write(bufferedImage,"PNG", imageFile);
return imageFile;
}
catch(IOException e){
e.printStackTrace();
return null;
}
}
I am a beginner, this is my first time using play, and first time using databases. Any advice to get this to work would be greatly appreciated.
我是初学者,这是我第一次使用 play,也是第一次使用数据库。任何使此工作正常的建议将不胜感激。
also, in my method getConvertedPicture() i have to specify the format type, is there anyway to get around this so the user can upload any type of picture they want.
此外,在我的方法 getConvertedPicture() 中,我必须指定格式类型,无论如何可以解决这个问题,以便用户可以上传他们想要的任何类型的图片。
回答by David
To convert bytes to an image without knowing the file type I usually do:
要在不知道文件类型的情况下将字节转换为图像,我通常会这样做:
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(bais);
That will return a BufferedImage that you can save into any picture format such as jpg.
这将返回一个 BufferedImage,您可以将其保存为任何图片格式,例如 jpg。
To write the image back out to a byte array:
要将图像写回字节数组:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte [] bytes = baos.toByteArray();
回答by ctype.h
To convert an array of bytes, i.e. byte[]
into an image, use getImage()
. Probably the easiest way to do this is to instantiate an ImageIcon
using the ImageIcon(byte[])
constructor, and then call getImage()
. This is illustrated in the method below, particularly the last line:
要将字节数组转换byte[]
为图像,请使用getImage()
. 可能最简单的方法是ImageIcon
使用ImageIcon(byte[])
构造函数实例化 an ,然后调用getImage()
. 下面的方法说明了这一点,特别是最后一行:
public Image createImage(){
byte[] b = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
0, 73, 69, 78, 68, -82, 66, 96, -126};
return new ImageIcon(b).getImage();
}
I think this can by used for png
, gif
, bmp
, and jpg
images. The byte array does not have to be hard-coded, as in this example.
我认为这可以通过使用png
,gif
,bmp
,和jpg
图像。字节数组不必硬编码,如本例所示。
Additionally, new ImageIcon("image.png").getImage()
can be used to load a displayable image from a file, where image.png
is the filename.
此外,new ImageIcon("image.png").getImage()
可用于从文件加载可显示图像,其中image.png
是文件名。
回答by Real Pongsakorn Jaranupong
Try to use
尝试使用
byte[] byteArray=null; //need to initialize it
ImageIcon imageIcon = new ImageIcon(byteArray);
imageIcon.getImage();