gpt4 book ai didi

android - 如何使用 ScriptIntrinsicYuvToRGB(将 byte[] yuv 转换为 byte[] rgba)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:38 28 4
gpt4 key购买 nike

我有 byte[] yuvByteArray (从 Camera.PreviewCallback.onPreviewFrame 方法捕获的 540x360 图像并转储到 assets/yuv.bin 文件中)。我想转换 byte[] yuvbyte[] rgba数组,使用以下代码(基于 LivePreview android 示例)。

但我收到 outBytes在 forEach 之后用零填充的 rgba 数组并复制 out分配给 outBytes。我的代码有什么问题?


<p></p>

<pre><code>package hellorender;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicYuvToRGB;
import android.support.v8.renderscript.Type;
import android.widget.ImageView;
import hellorender.R;

import java.io.IOException;
import java.io.InputStream;

public class HelloRenderActivity extends Activity {

public static final int W = 540;
public static final int H = 360;
private RenderScript rs;
private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AssetManager assets = getAssets();
byte[] yuvByteArray = new byte[291600];
byte[] outBytes = new byte[W * H * 4];

InputStream is = null;
try {
is = assets.open("yuv.bin");
System.out.println("read: " + is.read(yuvByteArray));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

ImageView iv = (ImageView) findViewById(R.id.image);
rs = RenderScript.create(this);
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));


Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs))
.setX(W).setY(H)
.setYuvFormat(android.graphics.ImageFormat.NV21);
Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);


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

in.copyFrom(yuvByteArray);

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

out.copyTo(outBytes);

Bitmap bmpout = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888);
out.copyTo(bmpout);

iv.setImageBitmap(bmpout);
}

}
</code></pre>

最佳答案

yuv.bin 文件绝对是 NV21 格式,因为它在这里捕获 http://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html#onPreviewFrame

setYuvFormat 方法来自 API 级别 18,我将其删除

所以这段代码工作正常:

rs = RenderScript.create(this);
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));

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

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

in.copyFrom(yuvByteArray);

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

关于android - 如何使用 ScriptIntrinsicYuvToRGB(将 byte[] yuv 转换为 byte[] rgba),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20358803/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com