我已经集成了affectiva库并尝试使用相机流来获取情感。 我试图从返回的帧中获取位图,但它总是返回null.Below是代码。
byte[] imageBytes = ((Frame.ByteArrayFrame) frame).getByteArray();
我们有一些518 kb的数据但是当转换为图像时它返回null。
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
previewImage.setImageBitmap(bitmap);
我甚至尝试了下面的代码,但又是null位图
Bitmap bmp=frame.getOriginalBitmapFrame();
你能帮我从框架中获取位图吗?
答案 0 :(得分:0)
Affectiva的affdexme-android
示例应用程序中有一些示例转换方法:
public static Bitmap getBitmapFromFrame(@NonNull final Frame frame) {
Bitmap bitmap;
if (frame instanceof Frame.BitmapFrame) {
bitmap = ((Frame.BitmapFrame) frame).getBitmap();
} else { //frame is ByteArrayFrame
switch (frame.getColorFormat()) {
case RGBA:
bitmap = getBitmapFromRGBFrame(frame);
break;
case YUV_NV21:
bitmap = getBitmapFromYuvFrame(frame);
break;
case UNKNOWN_TYPE:
default:
Log.e(LOG_TAG, "Unable to get bitmap from unknown frame type");
return null;
}
}
if (bitmap == null || frame.getTargetRotation().toDouble() == 0.0) {
return bitmap;
} else {
return rotateBitmap(bitmap, (float) frame.getTargetRotation().toDouble());
}
}
public static Bitmap getBitmapFromRGBFrame(@NonNull final Frame frame) {
byte[] pixels = ((Frame.ByteArrayFrame) frame).getByteArray();
Bitmap bitmap = Bitmap.createBitmap(frame.getWidth(), frame.getHeight(), Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(pixels));
return bitmap;
}
public static Bitmap getBitmapFromYuvFrame(@NonNull final Frame frame) {
byte[] pixels = ((Frame.ByteArrayFrame) frame).getByteArray();
YuvImage yuvImage = new YuvImage(pixels, ImageFormat.NV21, frame.getWidth(), frame.getHeight(), null);
return convertYuvImageToBitmap(yuvImage);
}
/**
* Note: This conversion procedure is sloppy and may result in JPEG compression artifacts
*
* @param yuvImage - The YuvImage to convert
* @return - The converted Bitmap
*/
public static Bitmap convertYuvImageToBitmap(@NonNull final YuvImage yuvImage) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, yuvImage.getWidth(), yuvImage.getHeight()), 100, out);
byte[] imageBytes = out.toByteArray();
try {
out.close();
} catch (IOException e) {
Log.e(LOG_TAG, "Exception while closing output stream", e);
}
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
}
public static Bitmap rotateBitmap(@NonNull final Bitmap source, final float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}