How to get camera preview frame after every 500ms

后端 未结 2 533
暖寄归人
暖寄归人 2021-01-25 20:53

I am developing sample application which gives me color code of pointed image or object via Camera in android. My application is similar to this application and I am using this

2条回答
  •  爱一瞬间的悲伤
    2021-01-25 21:08

    You can calculate the elapsed time in onPreviewFrame(). For example:

        boolean isFirstTime = true;
        long startTime = 0;
        PreviewCallback callback = new PreviewCallback() {
            @Override
            public void onPreviewFrame(byte[] data, Camera camera) {
                // TODO Auto-generated method stub
                if (isFirstTime) {
                    isFirstTime = false;
                    startTime = SystemClock.currentThreadTimeMillis();
                    decodeYUV420SP(pixels, data, previewSize.width,  previewSize.height);  
                    listener.OnPreviewUpdated(pixels, previewSize.width, previewSize.height);
                }
                else {
                    long currentTime = SystemClock.currentThreadTimeMillis();
                    long elapsedTime = currentTime - startTime;
                    if (elapsedTime >= 500) { // trigger your event
                        startTime = currentTime;
                        decodeYUV420SP(pixels, data, previewSize.width,  previewSize.height);  
                        listener.OnPreviewUpdated(pixels, previewSize.width, previewSize.height);
                    }
                }
            }
        };
    

    Do not forget to reset the boolean value and start time when you switch the preview status.

提交回复
热议问题