java中图像的数据类型是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29208007/
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
What is the data type for images in java
提问by Srini Vasan
The below code is for get and set the attributes for string, like this what is the data type to define the image, profile_picture is the variable for image. I'm going to store the image in DB.
下面的代码用于获取和设置字符串的属性,像这样定义图像的数据类型是什么,profile_picture 是图像的变量。我要将图像存储在数据库中。
private Long id;
public String first_name() {
return first_name;
}
public void setfirst_name(String first_name) {
this.first_name =first_name;
回答by Razib
You may use either to represent a 2D image-
您可以使用任何一个来表示 2D 图像 -
Edit:For saving image in database you may convert BufferredImage
to byte[]
(and then save it as BLOB
in DB) using the following code snippet -
编辑:为了在数据库中保存图像,您可以使用以下代码片段转换BufferredImage
为byte[]
(然后将其保存为BLOB
DB) -
try{
BufferedImage originalImage =
ImageIO.read(new File("path/to/image/imag.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( originalImage, "jpg", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
//save imageInByte as blob in database
}catch(IOException e){
System.out.println(e.getMessage());
}finally{
baos.close();
//close database connection
}
回答by nom
URL iconURL = new URL("");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
Image i = icon.getImage();
Image i;
is the variable that stores the the image, You can use it from there.
Image i;
是存储图像的变量,您可以从那里使用它。
Cheers.
干杯。
Edit:I suggest that instead of saving the whole image in the database, just save the path to the image....
编辑:我建议不要将整个图像保存在数据库中,只需保存图像的路径....
回答by Joop Eggen
If you do no image processing in java, you could store the bytes, byte[]
or on database level (SQL BLOB, binary large object), a SerialBlob
(implements the interface Blob
).
如果在 java 中不进行图像处理,则可以存储字节,byte[]
或在数据库级别(SQL BLOB,二进制大对象), a SerialBlob
(实现接口Blob
)。
Maintaining the images as files with only the paths in the database, also has its merits. In a mixed approach you can read/write a file to a blob database column, just using Input/OutputStream which saves memory.
将图像保存为仅包含数据库中路径的文件,也有其优点。在混合方法中,您可以将文件读/写到 blob 数据库列,只需使用节省内存的 Input/OutputStream。
回答by govardhan
File f = new File("D:/New Doc_1.jpg");
int length = (int)f.length();
FileInputStream fis = new FileInputStream(f);
pstmt.setBinaryStream(3, fis,length);
int i = pstmt.executeUpdate();