java中如何通过socket发送图像数据类型
我真的很困惑如何通过套接字发送图像数据类型。请帮帮我。我已经研究了如何将图像数据类型转换为char[],但结果是0。使用write方法。尝试以下方法:java中如何通过socket发送图像数据类型,java,image,sockets,Java,Image,Sockets,我真的很困惑如何通过套接字发送图像数据类型。请帮帮我。我已经研究了如何将图像数据类型转换为char[],但结果是0。使用write方法。尝试以下方法: InputStream inp = new FileInputStream("image.png"); byte[] buffer=new byte[1024]; int readData; while((readData = inp.read(buffer))!=-1){ socketOutput.write(buffer,0,readData
InputStream inp = new FileInputStream("image.png");
byte[] buffer=new byte[1024];
int readData;
while((readData = inp.read(buffer))!=-1){
socketOutput.write(buffer,0,readData);
}
您可以使用此方法()将
图像
转换为缓冲图像
:
然后,您可以使用方法编写生成的
buffereImage
。Why char[]?它不应该是byte[]吗?你可以将图像转换成byte数组,然后将数组发送到socket,然后在另一端将byte数组转换成图像。我认为它可以用char[]或byte[]发送,对吗?当然是Java中的图像数据类型。如整数、字符或字符串等。char[]
包含字符,byte[]
包含字节。您的图像包含字节,而不是字符。我已经尝试过了,但是ImageIo.write必须使用RenderImageChar[]或byte[]之类的RenderImage来实现。我无法将图像转换为字符[]或字节[]。抱歉,我忘了告诉您我的图像是从相机获取的,而不是从文件获取的。我必须如何处理图像数据类型?您可以将图像保存到一个临时文件并传输文件。请帮助我理解max:@ChristoforusSurjoputro好了,我对每一行代码都发表了评论。非常感谢max。您太好了。:)公共缓冲区图像toBufferedImage(最终图像,最终整型){如果(BuffereImage的映像实例)返回(BuffereImage)映像;如果(VolatileImage的映像实例)返回((VolatileImage)映像).getSnapshot();final BuffereImage buffImg=new BuffereImage(image.getWidth(null),image.getHeight(null),type);返回buffImg;}我有一个问题。图像是如何转换为缓冲图像的?我没有在那个程序中看到做这项工作的。但是它工作得很好。。谢谢。:)@ChristoforusSurjoputro在我的代码中,这部分做转换部分:g2.drawImage(图像,null,null)代码>。如果你的代码有效(注释中的代码),这意味着你很幸运,已经有了BuffereImage或VolatileImage。
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) {}
}
}
}
}