我已经能够从我的相机预览中操作字节数组,并使用渲染脚本从 yuvtorb 更改字节。现在,我需要知道如何将 rgb 字节渲染回相机预览。我相信无法更新相机预览。我需要在我的纹理视图之上添加某种覆盖视图,然后将更新后的字节数组渲染到覆盖视图的表面。我需要一些关于我应该如何去做的方向。有人可以告诉我我应该怎么做。这是我到目前为止所尝试的。
Mainactivity.java
public class MainActivity extends Activity implements TextureView.SurfaceTextureListener, Camera.PreviewCallback {
private byte[] FrameData = null;
private Camera mCamera;
private TextureView mTextureView;
Context mContext;
private Camera.Size previewSize;
private ImageView mimageView;
private SurfaceTexture mTexture;
private int[] pixels;
private int pwidth;
private int pheight;
Renderscript rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mimageView = new ImageView(this);
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);
rv = new Renderscript();
setContentView(mTextureView);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
int mPreviewRate = Math.round((float)screenHeight / (float)screenWidth);
mCamera = Camera.open();
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.width*mPreviewRate, previewSize.height*mPreviewRate, Gravity.CENTER));
Camera.Parameters params = mCamera.getParameters();
if (params.getSupportedFocusModes().contains(
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
if (params.isAutoExposureLockSupported()) {
params.setAutoExposureLock(false);
}
mCamera.setParameters(params);
pwidth = previewSize.width*mPreviewRate;
pheight = previewSize.height*mPreviewRate;
try {
mCamera.setPreviewTexture(surface);
} catch (IOException t) {
}
mCamera.setPreviewCallback(this);
mCamera.startPreview();
mTextureView.setAlpha(1.0f);
mTextureView.setRotation(90.0f);
pixels = new int[pwidth*pheight];
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, the Camera does all the work for us
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
mCamera.setPreviewCallback(null);
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Update your view here!
mCamera.setPreviewCallback(this);
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
data = rv.convertYUV2RGB(MainActivity.this,data,pwidth,pheight);
// sendbytearraytondkforopencvprocessing(data);
}
static {
Native.register(MainActivity.class, "native-lib");
}
public native void toGrayScale(int pixels[], int len);
}
渲染脚本.java
public byte[] convertYUV2RGB(Context c,byte[] YUVArray, int H, int W){//W: 1280, H: 720
RenderScript rs = RenderScript.create(c);
ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(YUVArray.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(YUVArray);
byte[] RGBOut = new byte[W * H * 4];
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
out.copyTo(RGBOut);
return RGBOut;
}