防止在相机预览更新时闪烁

时间:2018-08-24 20:27:37

标签: android image-processing imageview renderscript uvc

我正在使用从USB摄像机接收到的帧(使用UVCCamera库),并且使用RenderScript应用模糊效果。帧格式为NV21。 RenderScript创建的模糊效果非常好。但是,当我想使用Zomato Filters来应用其他效果时,尽管应用了效果,但还是得到了闪烁的结果。

然后在onFrame方法中,我从USB摄像头获取帧并根据搜索栏值应用滤镜:

    private final IFrameCallback mIFrameCallback = new IFrameCallback() {
        @Override
        public void onFrame(final ByteBuffer frame) {
            frame.clear();
            synchronized (bitmap) {
//                bitmap.copyPixelsFromBuffer(frame);


                byte[] bytes = new byte[frame.remaining()];
                frame.get(bytes);
                MainActivity.this.allocationYUV.copyFrom(bytes);
                MainActivity.this.ApplyEffect = false;
                if (!MainActivity.this.ApplyEffect) {
                    MainActivity.this.intrinsicYuvToRGB.setInput(MainActivity.this.allocationYUV);
                    MainActivity.this.intrinsicYuvToRGB.forEach(MainActivity.this.allocationBlur);
                    MainActivity.this.intrinsicBlur.forEach(MainActivity.this.allocationBlur, MainActivity.this.allocationOut);                   

                }
                MainActivity.this.allocationOut.syncAll(128);

                //Here, apply the three other effects
                int brightness = mSeekbar1.getProgress();
                int contrast = mSeekbar2.getProgress();
                int saturation = mSeekbar3.getProgress();

                //The following checks are done to prevent unnecessary image processing
                Filter myFilter = new Filter();
                if(brightness != 0) {
                        BrightnessSubfilter bs = new BrightnessSubfilter(brightness * 32);
                        bs.setTag("brightness");
                        myFilter.addSubFilter(bs);
                }
                else { //If 0, then remove the filter altogether
                    myFilter.removeSubFilterWithTag("brightness");
                }
                if(contrast != 0) {
                        ContrastSubfilter cs = new ContrastSubfilter(contrast * 32);
                        cs.setTag("contrast");
                        myFilter.addSubFilter(cs);
                        mContrastChanged = false;
                }
                else {
                    myFilter.removeSubFilterWithTag("contrast");
                }

                if(saturation != 0) {
                        SaturationSubfilter ss = new SaturationSubfilter(saturation * 32);
                        ss.setTag("saturation");
                        myFilter.addSubFilter(ss);
                        mSaturationChanged = false;
                }
                else {
                    myFilter.removeSubFilterWithTag("saturation");
                }
                mOutBitmap = myFilter.processFilter(bitmap);

                mImageView.post(mUpdateImageTask);

            }
        }
    };

    private final Runnable mUpdateImageTask = new Runnable() {
        @Override
        public void run() {
            synchronized (bitmap) {
                mImageView.setImageBitmap(mOutBitmap);
            }
        }
    };

我认为用于模糊处理的RenderScript分配非常典型,因此我没有添加这些分配以节省空间并降低复杂性。

enter image description here

0 个答案:

没有答案