[英]Sending BufferedImages Over Sockets
我想通過自定義類在網絡上發送緩沖的圖像。
我目前只是writeObject和readObject來獲取我的課程。
發送當前正在執行的圖像:
((DataBufferByte) i.getData().getDataBuffer()).getData();
如何將其轉換回BufferedImage?
有沒有更好的方法可以做到這一點?
我發送的課程如下:
public class imagePack{
public byte[] imageBytes;
public String clientName;
public imagePack(String name, BufferedImage i){
imageBytes = ((DataBufferByte) i.getData().getDataBuffer()).getData();
clientName = name;
}
public BufferedImage getImage(){
//Do something to return it}
}
再次感謝
如果要將其轉換回BufferedImage,還必須知道其寬度,高度和類型。
class imagePack {
public byte[] imageBytes;
public int width, height, imageType;
public String clientName;
public imagePack(String name, BufferedImage i) {
imageBytes = ((DataBufferByte) i.getData().getDataBuffer())
.getData();
width = i.getWidth();
height = i.getHeight();
imageType = i.getType();
clientName = name;
}
public BufferedImage getImage() {
if (imageType == BufferedImage.TYPE_CUSTOM)
throw new RuntimeException("Failed to convert.");
BufferedImage i2 = new BufferedImage(width, height, imageType);
byte[] newImageBytes = ((DataBufferByte) i2.getData()
.getDataBuffer()).getData();
System.arraycopy(imageBytes, 0, newImageBytes, 0, imageBytes.length);
return i2;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.