我已经能够从我的相机预览中操纵字节数组,并使用renderscript从yuvtorgb更改字节。现在,我需要知道如何将rgb字节渲染回相机预览。我相信无法更新相机预览。我将需要在我的textureview顶部的某种叠加视图,然后将更新的字节数组呈现到叠加视图的表面。我需要一些指导我应该如何去做。有人可以建议我应该怎么做。这是我到目前为止尝试过的。
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);
}
Renderscript.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;
}
答案 0 :(得分:0)
我相信无法更新相机预览。
是的。
我需要在我的textureview顶部放置某种叠加层视图,然后将更新的字节数组呈现到叠加层视图的表面。
您可以在OpenCV中看到类似的方法。他们的Android演示显示RGB预览。他们不使用renderscript,但这没关系。无论如何,帧频(取决于CPU功率和相机分辨率)通常不够平滑。
要获得更好的性能,您可以使用纹理视图,或者更确切地说,可以通过直接在EGL纹理上设置像素(glTexSubImage2D)来使用GPU。参见example。
但是,如果这样做,实际上会浪费大量时间在CPU和GPU之间传递数据:一次使用renderscript,然后再次设置纹理。相反,您可以使用OpenGL直接设置YUV像素,然后employ a shader to convert them to RGB for display。