我真的很困惑如何通过套接字发送图像数据类型。请帮帮我。我搜索了如何将图像数据类型转换为char [],但结果为0.如何通过java中的套接字发送图像数据类型
回答
您可以转换Image
到BufferedImage
用此方法(source):
public BufferedImage toBufferedImage(final Image image, final int type) {
//Test if image does not need conversion
if (image instanceof BufferedImage)
return (BufferedImage) image;
//Check if image can be converted easily
if (image instanceof VolatileImage)
return ((VolatileImage) image).getSnapshot();
//loadImage method ensures that the image has loaded fully (it could be from the web or something). If you are sure that when this method is called - the image is loaded, you can remove this line and whole method.
loadImage(image);
//Create new BufferedImage with the same width and height and given data type (see constants in BufferedImage API)
final BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
//Get graphics out of our new BufferedImage. Graphics2D is used to draw something on the image
final Graphics2D g2 = buffImg.createGraphics();
//Use Graphics2D to draw our Image contents on top of BufferedImage
g2.drawImage(image, null, null);
//We no longer need our graphics object as we drawn everything we wanted
g2.dispose();
//Return BufferedImage
return buffImg;
}
//Method that ensures that the image was loaded succesfully
private void loadImage(final Image image) {
//Inner class implementing the ImageObserver interface. It will be used to track the image loading progress
class StatusObserver implements ImageObserver {
boolean imageLoaded = false;
//Each time an image updates - it will call this method
public boolean imageUpdate(final Image img, final int infoflags,
final int x, final int y, final int width, final int height) {
//When flags contains ALLBITS flag - that means that the image was fully loaded.
if (infoflags == ALLBITS) {
synchronized (this) {
//Therefore we set status to true
imageLoaded = true;
//And notify anyone who was waiting for our job to be done
notify();
}
return true;
}
return false;
}
}
//Then we create the observer itself
final StatusObserver imageStatus = new StatusObserver();
//We aquire it's monitor with this synchronized block. This will allow us to "wait" for it to complete loading (see notify() in StatusObserver)
synchronized (imageStatus) {
//Basically if image is loaded - it will have it's width and height set
if (image.getWidth(imageStatus) == -1 || image.getHeight(imageStatus) == -1) {
//While status observer is not loaded
while (!imageStatus.imageLoaded) {
try {
//We wait for status observer to notify us
imageStatus.wait();
} catch (InterruptedException ex) {}
}
}
}
}
然后,您可以用ImageIO.write()方法写所产生的BufferedImage
。
使用ImageIO写入方法。
我试过了,但ImageIo.write必须用像char []或byte []这样的renderedimage实现。我无法将图像转换为char []或byte []。 –
尝试类似:
InputStream inp = new FileInputStream("image.png");
byte[] buffer=new byte[1024];
int readData;
while((readData = inp.read(buffer))!=-1){
socketOutput.write(buffer,0,readData);
}
对不起,我忘了告诉你所有我的图像来自相机,而不是从文件。我如何处理图像数据类型? –
您可以将图像保存到临时文件并传输文件。 – CloudyMarble
- 1. 如何通过linux套接字发送图像数据
- 2. 通过套接字发送图像
- 3. c通过套接字发送图像
- 4. 通过Java中的套接字发送和接收数据
- 5. 如何通过套接字连接发送不同的数据类型
- 6. 如何通过JAVA中的套接字发送类型为File []的变量
- 7. 如何通过套接字发送和接收图像
- 8. 通过套接字发送NULL数据
- 9. 通过UDP套接字发送数据
- 10. Android通过套接字发送数据
- 11. 通过java中的套接字发送大量数据
- 12. 通过java中的套接字发送数据
- 13. 如何通过java中的套接字发送html文件及其图像
- 14. 如何读取要通过套接字发送的图像?
- 15. 如何通过套接字发送图像
- 16. 如何通过套接字发送图像对象...?
- 17. Java通过套接字发送文件
- 18. Java - 通过套接字发送证书
- 19. 通过Java套接字发送文件
- 20. 如何通过套接字发送/接收数据文件?
- 21. 如何通过套接字发送/接收二进制数据?
- 22. 通过Python中的套接字发送各种类型的数据
- 23. 通过java套接字发送数据的编码问题?
- 24. 如何通过PHP的unix套接字发送数据报?
- 25. iOS:用于发送图像数据的套接字通信
- 26. 通过C++中的套接字发送图像
- 27. java - 通过套接字连续发送数据
- 28. 通过套接字从Objective-C发送数据到Java
- 29. 通过Java套接字连续发送数据
- 30. 通过数据报套接字发送一个对象java
什么是“图像数据类型”? – bezmax
为什么字符[]?它不应该是一个字节[]? – Thilo
您可以将图像转换为字节数组,并将该数组发送到套接字并将字节数组转换为另一端的图像。 –