[英]Convert BufferedImage to aws…rekognition.model.Image
I'm playing around with the Amazon Rekognition. 我正在玩Amazon Rekognition。 I found a really nice/easy library to take an image from my webcam which works like this: 我找到了一个非常不错/很容易的库 ,可以从我的网络摄像头拍摄图像,其工作方式如下:
BufferedImage bufImg = webcam.getImage();
I'm then trying to convert this BufferedImage
to a com.amazonaws.services.rekognition.model.Image
, which is what must be submitted to the Rekognition library. 然后,我试图将此BufferedImage
转换为com.amazonaws.services.rekognition.model.Image
,这是必须提交给Rekognition库的内容。 This is what I'm doing: 这就是我在做什么:
byte[] imgBytes = ((DataBufferByte) bufImg.getData().getDataBuffer()).getData();
ByteBuffer byteBuffer = ByteBuffer.wrap(imgBytes);
return new Image().withBytes(byteBuffer);
However when I try and do some API call to Rekognition with the Image
, I get an Exception: 但是,当我尝试使用Image
对Rekognition进行一些API调用时,出现异常:
com.amazonaws.services.rekognition.model.InvalidImageFormatException: Invalid image encoding (Service: AmazonRekognition; Status Code: 400; Error Code: InvalidImageFormatException; Request ID: X)
The docs state that the Java SDK will automatically base64 encode the bytes. docs声明Java SDK将自动对字节进行base64编码。 In case, something weird was happening, I tried base64 encoding the bytes before converting: 万一发生奇怪的事情,我尝试在转换之前尝试对字节进行base64编码:
imgBytes = Base64.getEncoder().encode(imgBytes);
However, the same Exception ensues. 但是,也会发生相同的异常。
Any ideas? 有任何想法吗? :) :)
I tried encoding the image to a JPG (Rekognition supports PNG or JPG formats) and it solved the problem. 我尝试将图像编码为JPG(Rekognition支持PNG或JPG格式),它解决了问题。
BufferedImage bufImg = webcam.getImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufImg, "jpg", baos);
ByteBuffer byteBuffer = ByteBuffer.wrap(baos.toByteArray());
return new Image().withBytes(byteBuffer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.