我正在尝试检测用相机拍摄的照片中的文字,但没有运气。
我正在使用的代码是:
AWSCredentials credentials = new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
return "some access key id";
}
@Override
public String getAWSSecretKey() {
return "some secret key";
}
};
File file = new File(photoFilePath);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
Timber.e(e);
} catch (IOException e) {
Timber.e(e);
}
AmazonRekognition rekognitionClient = new AmazonRekognitionClient(credentials);
byte [] base64 = android.util.Base64.encode(bytes, Base64.DEFAULT);
Image image = new Image().withBytes(ByteBuffer.wrap(base64));
DetectTextRequest detectTextRequest = new DetectTextRequest().withImage(image);
Observable.create((Observable.OnSubscribe<String>) observer -> {
try {
DetectTextResult result = rekognitionClient.detectText(detectTextRequest);
List<TextDetection> labels = result.getTextDetections();
String alllabels = "";
for (TextDetection detection : labels) {
alllabels += detection.getDetectedText();
}
observer.onNext(alllabels);
observer.onCompleted();
} catch (AmazonServiceException e) {
Timber.e(e);
observer.onError(e);
} catch (AmazonClientException e) {
Timber.e(e);
observer.onError(e);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
@Override
public void onNext(String item) {
System.out.println("Next: " + item);
}
@Override
public void onError(Throwable error) {
System.err.println("Error: " + error.getMessage());
}
@Override
public void onCompleted() {
System.out.println("Sequence complete.");
}
});
}
这会产生消息异常
Failed to upload image; the format is not supported
当不在 base64 中编码字节时 - 它会产生奇怪的输出,其中检测到的每个文本都用逗号分隔,例如
S, !!:, 8, anons SAr, !!:, S, 8, anons, SAr,
或者
8B、8B
我的例子可能有什么问题?
即使使用同一张照片,也可以使用对 S3 对象的引用 - 一切正常。