我有byte[] yuvByteArray
(540x360圖像捕獲從Camera.PreviewCallback.onPreviewFrame
方法和轉儲到assets/yuv.bin
文件)。我想使用以下代碼(基於LivePreview android示例)將byte[] yuv
轉換爲byte[] rgba
數組。如何使用ScriptIntrinsicYuvToRGB(轉換byte [] yuv到byte [] rgba)
但我收到outBytes
rgba數組填充零後forEach和複製out
分配給outBytes。我的代碼有什麼問題?傳遞的ScriptIntrinsicYubToRGB
構造
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);
}
}
很高興你能夠得到它的工作。不幸的是,內部函數和大多數RenderScript沒有很好的記錄,所以很容易出現這樣的奇怪事情。 –
我們如何獲得轉換的RGB字節數組? –
與使用純java完成rutine相比,這是非常瘋狂的! – slott