我真的很困惑如何通過套接字發送圖像數據類型。請幫幫我。我搜索瞭如何將圖像數據類型轉換爲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
您可以將圖像轉換爲字節數組,並將該數組發送到套接字並將字節數組轉換爲另一端的圖像。 –