변환하는 방법을 바이트 배열을 이미지 버퍼
-
21-12-2019 - |
문제
I have a server-side java 코드는 바이트 배열에서 클라이언트입니다.일부를 수행하기 위해 이미지 처리,내가 변환이 필요한 바이트 배열로 BufferedImage
.내가 코드에 의해 보십시오 여기:
public void processImage(byte[] data) {
ByteArrayInputStream stream = new ByteArrayInputStream(data);
BufferedImage bufferedImage;
bufferedImage = ImageIO.read(stream);
// bufferedImage is null
//...
}
하지만 이 작동하지 않;bufferedImage 은 null 입니다.에 따라 ImageIO 설명서:
지 않은 경우 등록
ImageReader
클레임을 읽을 수 있어 결과림null
이 반환됩니다.
게 하려면 어떻게 합니까 ImageReader
어떤 이미지 유형이 그것입니다.예를 들어 알고 있는 경우 이미지를 JPEG(는 그것은,나의 케이스에서),나는 무엇을 해야 하나?
편집: 감사에 대한 제안하는 파일은 가능성이 높지 않은 JPEG 형식입니다.이것은 클라이언트 측의 코드가 보내는 데이터로 문자열을 통해 서버
import org.json.JSONObject;
// Client-side code that sends image to server as String
public void sendImage() {
FileInputStream inputStream = new FileInputStream(new File("myImage.jpg"));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((bytesRead = inputStream.read(b)) != -1) {
byteStream.write(b,0,bytesRead);
}
byte[] byteArray = byteStream.toByteArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("data",new String(byteArray));
// ... more code here that sends jsonObject in HTTP post body
}
이는 서버쪽의 코드를 호출하는 processImage()함수:
// Server-side code that calls processImage() function
public void handleRequest(String jsonData) {
JSONObject jsonObject = new JSONObject(jsonData);
processImage(jsonObject.getString("data").getBytes());
}
해결책
대부분은 바이트 배열 지 을 포함 JPEG 이미지입니다.(예를 들어,만약 당신이 시도를 다운로드할 수 있습 HTML 문서를 주는 오류 진단.) 이 경우에는,당신을 찾을 필요가있을 것이 무엇이 원인을 해결합니다.
그러나,당신은"알고있다"하는 바이트 배열을 포함한 이미지로 지정된 형식으로,당신이 뭔가를 할 수 있습니다:
- 사
ImageIO.getImageReadersByFormatName
나ImageIO.getImageReadersByMIMEType
을 얻Iterator<ImageReader>
. - 당겨 처음
ImageReader
서Iterator
. - Create an
MemoryCacheImageInputStream
포장ByteArrayInputStream
에 대한 형식입니다. - 사
ImageReader.setInput
을 연결하는 독자ImageInputStream
. - 사
ImageReader.read
을 얻BufferedImage
.
제휴하지 않습니다 StackOverflow