skia项目运行在Android.4.2,Android 4.2和Android 2.2有区别吗?
Posted
技术标签:
【中文标题】skia项目运行在Android.4.2,Android 4.2和Android 2.2有区别吗?【英文标题】:skia project run in Android.4.2, Does there exists difference define in Android 4.2 and Android 2.2? 【发布时间】:2013-12-09 13:08:00 【问题描述】:我尝试在 Andorid 上显示 YUV 视频 安卓2.2版本可以 但是在 android 4.2 中,屏幕是全黑的
在项目中,我使用JAVA来显示图片的内容 并使用 jni 读取 YUV 并将 YUV 数据返回给 JAVA
以下是我的jni代码 #include "SkiaDemoJni.h" #include "mylog.h"
#include "GraphicsJNI.h"
#include "SkPaint.h"
#include "SkCanvas.h"
#include"opj_includes.h"
#include <wchar.h>
#include <android\log.h>
#include <android\bitmap.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);
【问题讨论】:
【参考方案1】:您需要确保您没有使用硬件渲染,方法是:
调用 view.setLayerType(LAYER_TYPE_SOFTWARE, null);或在 AndroidManifest.xml 的应用程序部分设置 android:hardwareAccelerated="false"
转到开发者选项,取消选中“强制 GPU 渲染”
【讨论】:
以上是关于skia项目运行在Android.4.2,Android 4.2和Android 2.2有区别吗?的主要内容,如果未能解决你的问题,请参考以下文章