Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将BuffereImage转换为aws…rekognition.model.Image_Java_Amazon Web Services_Bufferedimage_Amazon Rekognition - Fatal编程技术网

Java 将BuffereImage转换为aws…rekognition.model.Image

Java 将BuffereImage转换为aws…rekognition.model.Image,java,amazon-web-services,bufferedimage,amazon-rekognition,Java,Amazon Web Services,Bufferedimage,Amazon Rekognition,我在玩亚马逊的游戏。我找到了一个可以从我的网络摄像头中拍摄图像的方法,它的工作原理如下: BufferedImage bufImg = webcam.getImage(); 然后我尝试将这个buffereImage转换为com.amazonaws.services.rekognition.model.Image,这是必须提交到rekognition库的内容。这就是我正在做的: byte[] imgBytes = ((DataBufferByte) bufImg.getData().getDat

我在玩亚马逊的游戏。我找到了一个可以从我的网络摄像头中拍摄图像的方法,它的工作原理如下:

BufferedImage bufImg = webcam.getImage();
然后我尝试将这个
buffereImage
转换为
com.amazonaws.services.rekognition.model.Image
,这是必须提交到rekognition库的内容。这就是我正在做的:

byte[] imgBytes = ((DataBufferByte) bufImg.getData().getDataBuffer()).getData();
ByteBuffer byteBuffer = ByteBuffer.wrap(imgBytes);
return new Image().withBytes(byteBuffer);
然而,当我尝试使用
图像
执行一些API调用以重新命名时,我遇到了一个异常:

com.amazonaws.services.rekognition.model.InvalidImageFormatException: Invalid image encoding (Service: AmazonRekognition; Status Code: 400; Error Code: InvalidImageFormatException; Request ID: X)
Java SDK将自动对字节进行base64编码的状态。万一发生了奇怪的事情,我尝试在转换之前对字节进行base64编码:

imgBytes = Base64.getEncoder().encode(imgBytes);
然而,同样的例外情况随之而来


有什么想法吗?:)

我尝试将图像编码为JPG(Rekognion支持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);

我尝试将图像编码为JPG(Rekognion支持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);