Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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 在Android中播放yuv帧_Java_Android_Java Native Interface - Fatal编程技术网

Java 在Android中播放yuv帧

Java 在Android中播放yuv帧,java,android,java-native-interface,Java,Android,Java Native Interface,嗨,我的任务是在android中显示一组解码帧。在我的本地代码中,我有一个字符(char)指针,它保存解码帧的地址。我想在我的设备上显示此帧,因此得到以下提示: 因此,在我的活动类中,我编写了以下函数: public void displayFrame(byte[] data, int fwidth,int fheight){ ImageView frameImgView=(ImageView) findViewById(R.id.imageView2); ByteArray

嗨,我的任务是在android中显示一组解码帧。在我的本地代码中,我有一个字符(char)指针,它保存解码帧的地址。我想在我的设备上显示此帧,因此得到以下提示: 因此,在我的活动类中,我编写了以下函数:

public void displayFrame(byte[] data, int fwidth,int fheight){

    ImageView frameImgView=(ImageView) findViewById(R.id.imageView2);

    ByteArrayOutputStream out=new ByteArrayOutputStream();
    YuvImage yuvimg=new YuvImage(data, ImageFormat.NV21, fwidth, fheight, null);
    yuvimg.compressToJpeg(new Rect(0, 0, fwidth, fheight), 100, out);
    byte[] imageBytes = out.toByteArray();
    Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
    frameImgView.setImageBitmap(image);
    return;
}
JNIEXPORT jint JNICALL Java_com_example_decoder_Decoder_mainFunction(JNIEnv *env, jclass jobj, jint argc, jstring argv1, jstring argv2);
因此,从我的本地代码中,我需要在java中调用这个函数。从链接: 我学会了如何从本机c调用java函数。 然而,要调用java函数,我们需要一个环境变量来实现这一点。Im my java class i已将主函数声明为本机函数,并为该主函数生成了一个标头:

public void displayFrame(byte[] data, int fwidth,int fheight){

    ImageView frameImgView=(ImageView) findViewById(R.id.imageView2);

    ByteArrayOutputStream out=new ByteArrayOutputStream();
    YuvImage yuvimg=new YuvImage(data, ImageFormat.NV21, fwidth, fheight, null);
    yuvimg.compressToJpeg(new Rect(0, 0, fwidth, fheight), 100, out);
    byte[] imageBytes = out.toByteArray();
    Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
    frameImgView.setImageBitmap(image);
    return;
}
JNIEXPORT jint JNICALL Java_com_example_decoder_Decoder_mainFunction(JNIEnv *env, jclass jobj, jint argc, jstring argv1, jstring argv2);
但是,我的main函数调用一个函数“fwriter(somepointer,somepointer)”,该函数有一个指针作为参数。如何获取相同的环境变量。我知道,在java类中,我需要将“fwriter”函数声明为本机函数,但我如何表示指针

我的c函数是:

void fwriter(int *ptr, char *ptr)
{
  ....
}
在我的java类中,如何将此函数声明为本机函数?? 请帮忙。 在java/Android中演示YUV的任何其他方法也将受到赞赏。
谢谢。

虽然JPEG编码和解码在大多数手机上可能是硬件加速的,但我希望您自己编写的循环的简单RGB->YUV转换要快得多(即使在Java中不使用本机库也足够快)而且更简单

这是公式。如果使用OpenGLE显示,请考虑使用A来进行转换。

最后,这里是您可以在CPU上使用的仅限整数的代码:

Ytmp =      4768 * (Y - 16);
R = (Ytmp + 6537 * (V - 128)) >> 12;
G = (Ytmp - 3330 * (V - 128) - 1602 * (U - 128)) >> 12;
B = (Ytmp + 8266 * (U - 128)) >> 12;

您可以使用RenderScript内在函数将YUV图像转换为位图

您可以准备这些对象一次:

RenderScript rs = RenderScript.create(getContext());
ScriptIntrinsicYuvToRGB yuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));

Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length);
Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(fwidth).setY(fheight);
Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);

Bitmap image = Bitmap.createBitmap(fwidth, fheight, Bitmap.Config.ARGB_8888);
对于执行转换的每个帧:

in.copyFrom(data);

yuvToRgb.setInput(in);
yuvToRgb.forEach(out);

out.copyTo(image);

谢谢你的回复+为了你的努力。。。但我认为使用openGLES也会减慢进程。所以我需要用c代码本身做一些事情。我是说从yuv到rgb的转换。但是,如果我的yuv是yuv 4:4:4,我可以直接应用公式并转换为相应的rgb。但我的是YUV 4:2:0,所以请你建议我如何将其转换为rgb格式。我将等待答复。提前感谢…@Zax 1)如果您的目的是显示GPU,那么使用GPU将是最快的;2) 我自己为商业项目编写了代码,即使在4:2:0中,您也可以直接应用该公式,只需每隔一帧增加一些指针;3) 我将粘贴在经典的整数数学yuv转换中,以便快速回复。是的,我的主要目的是展示它。在我的本地代码中,我正在解码视频,解码的缓冲区是yuv格式的。所以我的主要工作是展示这个yuv框架。每一次迭代都会对一帧进行解码,以显示它,使其像视频一样稳定。因此,请帮助我们解决同样的问题。您是否有将420转换为RGB的代码??如果是,请给我发邮件。我还尝试了一些代码,但输出的是一组彩色的行:(从哪个版本的Android可以使用此功能?Renderscript是在API级别11引入的,但ScriptInthincyuvtorgb是在API级别17中添加的。)