问题描述
我有字节[] yuvByteArray
(从相机。previewCallback.on previewFrame $ C $拍摄的540x360的图像C>方法和倾倒进
资产/ yuv.bin
文件)。我想转换字节[] YUV
到字节[] RGBA
阵列,使用下面的code(基于在现场preVIEW Android的例子)。
I have byte[] yuvByteArray
(540x360 image captured from Camera.PreviewCallback.onPreviewFrame
method and dumped into assets/yuv.bin
file). I want to convert byte[] yuv
to byte[] rgba
array, using the following code (based on LivePreview android example).
不过,我收到 outBytes
RGBA阵列用零填充的forEach和复制后出
分配outBytes。有什么毛病我的code?
But I receive outBytes
rgba array filled with zeros after forEach and copying out
allocation to outBytes. What's wrong with my 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);
}
}
推荐答案
yuv.bin文件绝对是NV21格式,因为它抓住此处<一个href="http://developer.android.com/reference/android/hardware/Camera.$p$pviewCallback.html#on$p$pviewFrame" rel="nofollow">http://developer.android.com/reference/android/hardware/Camera.$p$pviewCallback.html#on$p$pviewFrame
yuv.bin file is definitely in NV21 format, as it captures here http://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html#onPreviewFrame
setYuvFormat方法是从API级别18,我删除它
setYuvFormat method is from API level 18, I removed it
所以这个code正常工作:
So this code works fine:
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);
这篇关于如何使用ScriptIntrinsicYuvToRGB(转换byte []的YUV为byte [] RGBA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!