我尝试在Andorid上显示YUV视频在安卓2.2版本中,它是可以的然而,在Android 4.2中,屏幕是完全黑色的
在项目中,我使用JAVA来显示图像的内容使用jni读取YUV并将YUV数据返回JAVA
以下是我的jni代码#包括"SkiaDemoJni.h"#包括"mylog.h"
#include "GraphicsJNI.h"
#include "SkPaint.h"
#include "SkCanvas.h"
#include"opj_includes.h"
#include <wchar.h>
#include <androidlog.h>
#include <androidbitmap.h>
#define two_pi (6.283)
unsigned char cur_image_y[101376];
unsigned char cur_image_u[101376];
unsigned char cur_image_v[101376];
inline int32_t color(int32_t pColorY,int32_t pColorU,int32_t pColorV) {
int32_t pColorR;
int32_t pColorG;
int32_t pColorB;
pColorR = pColorY+1.4075*(pColorV-128);
pColorG = pColorY-0.3455*(pColorU-128)-0.7169*(pColorV-128);
pColorB = pColorY+1.7790*(pColorU-128);
if(pColorR>255)
pColorR=255;
if(pColorG>255)
pColorG=255;
if(pColorB>255)
pColorB=255;
if(pColorR<0)
pColorR=0;
if(pColorG<0)
pColorG=0;
if(pColorB<0)
pColorB=0;
return 0xFF000000 | ((pColorB << 16) & 0x00FF0000) | ((pColorG << 8) & 0x0000FF00) |
((pColorR) & 0x000000FF);
}
void Java_com_whtr_example_skiademo_SkiaView_renderHello(JNIEnv *env, jobject thizz,
jobject canvas, jint FrameNum,jobject rect)
{
jclass bitmapConfig = env->FindClass("android/graphics/Bitmap$Config");
jfieldID rgb565FieldID = env->GetStaticFieldID(bitmapConfig, "ARGB_8888",
"Landroid/graphics/Bitmap$Config;");
jobject rgb565Obj = env->GetStaticObjectField(bitmapConfig, rgb565FieldID);
jclass bitmapClass = env->FindClass("android/graphics/Bitmap");
jmethodID createBitmapMethodID = env->GetStaticMethodID(bitmapClass,"createBitmap",
"(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
jobject bitmapObj = env->CallStaticObjectMethod(bitmapClass, createBitmapMethodID,
352, 288, rgb565Obj);//width,height
int a[101376]={0};
AndroidBitmapInfo info;
int* pixels;
int ret;
ret = AndroidBitmap_getInfo(env, bitmapObj, &info);
ret = AndroidBitmap_lockPixels(env, bitmapObj, (void**)&pixels);
int width=(int)info.width;
int height=(int)info.height;
SkBitmap bmp = SkBitmap();
bmp.setConfig(SkBitmap::kARGB_8888_Config,width,height);
bmp.setPixels((void*)pixels);
SkCanvas background(bmp);
SkCanvas* canv = GraphicsJNI::getNativeCanvas(env, canvas);
if (!canv)
{
LOGE("!canv");
return;
}
systemPara sys;
sys.width = 352;
sys.height = 288;
sys.frameNO = 1;
sys.startFrm = 0;
sys.inFileName = new char[strlen("/mnt/sdcard/out_352x288_P420_final.yuv")+1];
strcpy(sys.inFileName, "/mnt/sdcard/out_352x288_P420_final.yuv");
for(int i=0;i<sys.frameNO;i++){
//sys.startFrm = i;
YUV_play_read(cur_image_y, cur_image_u, cur_image_v, sys.inFileName, sys.width, sys.height, FrameNum);
for(int x=0;x<sys.width;x++){
for(int y=0;y<sys.height;y++){
a[x + y * 352] = color(cur_image_y[x + y * 352], cur_image_u[x + y * 352], cur_image_v[x + y * 352]);
}
}
}
pixels = a;
AndroidBitmap_unlockPixels(env, bitmapObj);
bmp.setPixels((void*)pixels);
canv->drawBitmap(bmp, 0, 0, NULL);
}
您需要通过以下方式确保您没有使用硬件渲染:
-
call view.setLayerType(LAYER_TYPE_SOFTWARE,null);或者在AndroidManifest.xml 的应用程序部分设置android:hardwareAccelerated="false"
-
转到开发者选项,并取消选中"强制GPU渲染"