我真的很困惑如何通过套接字发送图像数据类型。请帮我。我已经搜寻了如何将Image数据类型转换为char [],但结果为0。
您可以使用以下方法将其转换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) {}
}
}
}
}
然后,您可以BufferedImage
使用ImageIO.write()方法写入结果。
我有一个数组类型的。我必须在python中通过流/TCP套接字发送它。然后我必须在接收端接收相同的阵列。
我必须对FTP服务器进行编码,而且我在数据传输方面遇到了问题。 我在linux上使用ftp命令来测试它,我目前正在使用,它发送工作目录中的文件/目录列表。一切正常(ftp打印文件列表),除了ftp打印以下警告: 我想删除该警告,我认为ftp需要二进制数据,但我不知道如何通过套接字发送这样的数据,我目前正在使用最基本的方式发送结果:
这是客户端类的一部分(名为EasyDataReceive): 这是我得到的错误: 线程“main”java.net.socketException中的异常:在java.net.socketinputstream.read(未知源)在java.net.socketinputstream.read(未知源)在java.net.socketinputstream.read(未知源)在java.net.s
问题内容: 我使用这里的代码通过套接字发送单个文件。但是,我需要能够通过套接字发送多个文件(基本上是目录中的所有文件),并使客户端识别文件之间的分隔方式。坦白说,我完全不知所措。任何提示都会有所帮助。 注意1: 我需要一种以连续流方式发送文件的方法,客户端可以将其分离成单独的文件。它不能依赖客户端的个别请求。 注意2: 我很肯定会回答一个问题,不,这 不是 家庭作业。 编辑 建议我可以在文件本身之
问题内容: 我已经看到了许多在Java中通过套接字发送序列化数据的示例,但是我想要的只是发送一些简单的整数和字符串。而且,问题是我正在尝试将这些信息传递给用C编写的二进制文件。 因此,最重要的是:如何在Java中通过套接字发送一些字节? 问题答案: 我真的建议不要直接使用Java Sockets库。我发现Netty(来自JBoss)非常易于实现且功能强大。Netty ChannelBuffer类带
我试着做一个简单的服务器-客户机套接字通信,但服务器似乎无法正常工作。无论何时发送或接收,我都会收到此错误: 非插座上的插座操作:非插座上的插座操作 奇怪的是,这只会在服务器发送时出现。客户似乎还可以: