java图⽚传输⽅式_Java图像传输⽅法
在研发B/S视频系统时候涉及到了图⽚在⽹络中的传输,研究了⼀些资料,也积累了部分经验:
⼀。序列化后传输
图像对象可以编码成指定图像格式⽂件保存在硬盘上,需要时再对其进⾏解码读⼊内存。但是除了这样还有别的办法可以将图像对象保存在硬盘上吗?熟悉Java I/O 的⼈也许可以想到采⽤对象序列化(Object serialization). BufferedImage 提供⼀般图像管理
BufferedImage 对象包括另外两个对象:Raster 和 ColorModel。Raster 对象包含另外两个对象:DataBuffer 和 SampleModel。不幸的是,他们都没有实现序列化所必需的 Serializable 接⼝,所以⽆法直接对他们进⾏对象序列化。 JAI 的 包⾥有⼀个类 SerializableRenderedImage,这个类实现了RenderedImage, Serializable 接⼝,可以将 RanderedImage 对象作为构造函数的参数实例化⼀个可以序列化的图像对象。
查看JDK的⽂档可以知道⽆论 Java 2D 中的 BufferedImage 还是 JAI 中的 PlanarImage 都实现了 RenderedImage 接⼝,也就是说所有实现⾃ RenderedImage
接⼝的对象均可作为参数包装出⼀个 SerializableRenderedImage 类型对象,将其序列化
下⾯是⼀个简单的例⼦说明了这个类的使⽤⽅法:
查看复制到剪切板打印
import java.io.*;
*;
import java.awt.image.*;
public class SomeSerializableClass implements Serializable ...{
protected transient RenderedImage image;
public SomeSerializableClass(RenderedImage image) ...{
this.image = image;
}
// Serialization method.
private void writeObject(ObjectOutputStream out) throws
IOException ...{
out.defaultWriteObject();
out.writeObject(new SerializableRenderedImage(image, true));
}
// Deserialization method.
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException ...{
in.defaultReadObject();
image = (RenderedImage) in.readObject();
}
}
import java.io.*;
*;
import java.awt.image.*;
public class SomeSerializableClass implements Serializable ...{
protected transient RenderedImage image;
public SomeSerializableClass(RenderedImage image) ...{
this.image = image;
}
// Serialization method.
private void writeObject(ObjectOutputStream out) throws IOException ...{
out.defaultWriteObject();
out.writeObject(new SerializableRenderedImage(image, true));
}
// Deserialization method.
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException ...{
in.defaultReadObject();
image = (RenderedImage) in.readObject();
}
}
⼆。以字节流的⽅式传输
1.图像编码
因为图像编码解码主要⽬的是针对图像在⽹络中的传输,所以编码之后的图像不必保存在硬盘上,可以直接放⼊⼀个字节数组。查看复制到剪切板打印
public byte[] getCompressedImage(BufferedImage image){
byte[] imageData = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
imageData = ByteArray();
} catch (IOException ex) {
imageData = null;
}
return imageData;
}
public byte[] getCompressedImage(BufferedImage image){
byte[] imageData = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
imageData = ByteArray();
} catch (IOException ex) {
imageData = null;
}
return imageData;
}
对象图片高清
2。图像解码
接收端接收到表⽰图像数据的字节数组后,对其进⾏解码,得到图像对象。因为我们在发送端将其编码成JPEG格式,所以可以直接在接收端使⽤ImageIO对其进⾏解码。
查看复制到剪切板打印
public BufferedImage getDecompressedImage(byte[] imageData){
try {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
ad(bais);
} catch (IOException ex) {
return null;
}
}
public BufferedImage getDecompressedImage(byte[] imageData){
try {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
ad(bais);
} catch (IOException ex) {
return null;
}
}
3.⽹络传输
因为图像编码之后是⼀个存在于内存中的字节数组,所以可以使⽤IO流的⽅式将其发送到⽹络的接收端,接收端建⽴
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论