Camera.open()阻止UI线程

时间:2015-01-09 21:27:44

标签: android android-camera

我看过所有可以找到的SO文章,但没有一个解决方案适合我。

当调用Camera.open()时,阻塞UI线程会有3秒(给定或接受)延迟。我试图把它放在后台线程中。我目前正在使用找到的解决方案here(粘贴在下面),但'wait'方法是同步的,所以它也会阻止UI线程。

我要做的是加载这个片段,显示一个进度微调器,直到相机很好去,然后在屏幕上显示相机,但是这个延迟正在杀死我,我似乎无法找到真的很好的解决方案。

我的片段:

public class BarcodeFinderFragment extends Fragment implements View.OnClickListener, Camera.AutoFocusCallback, Camera.PreviewCallback {

    private static final String CAMERA_THREAD_NAME = "CAMERA_THREAD_NAME";
    private Camera mCamera;

    private CamViewFinder mPreview;
    private Handler autoFocusHandler;
    private boolean previewing = true;
    private Button noScan;
    private Button noBarcode;
    private FrameLayout preview;
    private BarcodeFinderCallback callBack;
    private ImageScanner scanner;

    private CameraHandlerThread mThread = null;


    private BarcodeFinderCallback dummyCallback = new BarcodeFinderCallback() {
        @Override
        public void onNoScanClicked() {

        }

        @Override
        public void onNoBarcodeClicked() {

        }

        @Override
        public void finishActivity() {

        }

        @Override
        public void setActivityResult(Bundle bundle) {

        }

        @Override
        public void showProgressDialog(boolean showProgress) {

        }
    };

    public static BarcodeFinderFragment newInstance() {
        return new BarcodeFinderFragment();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            callBack = (BarcodeFinderActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_barcode_finder, container, false);

        noScan = (Button) view.findViewById(R.id.btnNoScan);
        noBarcode = (Button) view.findViewById(R.id.btnNobarcode);
        preview = (FrameLayout) view.findViewById(R.id.cameraPreview);
        noScan.setOnClickListener(this);
        noBarcode.setOnClickListener(this);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        autoFocusHandler = new Handler();

        //Instance barcode scanner
        scanner = new ImageScanner();
        scanner.setConfig(0, Config.X_DENSITY, 3);
        scanner.setConfig(0, Config.Y_DENSITY, 3);

        openCamera();

        mPreview = new CamViewFinder(getActivity(), mCamera, BarcodeFinderFragment.this, BarcodeFinderFragment.this);
        preview.addView(mPreview);
        callBack.showProgressDialog(false);
    }

    private void getCamera() {
        mCamera = null;
        try {
            mCamera = Camera.open();
        } catch (final Exception e) {
            Log.d("BarcodeFinderFragment", e.toString());
        }
    }

    private void openCamera() {
        if (mThread == null)
            mThread = new CameraHandlerThread(CAMERA_THREAD_NAME);

        synchronized (mThread) {
            mThread.openCamera();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        releaseCamera();
    }

    @Override
    public void onDetach() {
        super.onDetach();

        callBack = dummyCallback;
    }

    private Runnable doAutoFocus() {
        return new Runnable() {
            @Override
            public void run() {
                if (previewing) {
                    mCamera.autoFocus(BarcodeFinderFragment.this);
                }
            }
        };
    }

    private void releaseCamera() {
        if (mCamera != null) {
            previewing = false;
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }

        callBack.finishActivity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnNoScan:
                callBack.onNoScanClicked();
                break;

            case R.id.btnNobarcode:
                callBack.onNoBarcodeClicked();
                break;
        }
    }

    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        autoFocusHandler.postDelayed(doAutoFocus(), 1000);
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {

        final Camera.Parameters parameters = camera.getParameters();
        final Camera.Size size = parameters.getPreviewSize();

        final Image barcode = new Image(size.width, size.height, "Y800");
        barcode.setData(data);

        final int result = scanner.scanImage(barcode);

        if (result != 0) {
            previewing = false;
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();

            final SymbolSet syms = scanner.getResults();
            for (final Symbol sym : syms) {
                final Bundle bundle = new Bundle();
                bundle.putString("result", sym.getData());
                bundle.putString("codeType", "" + sym.getType());

                callBack.setActivityResult(bundle);
            }
        }
    }

    public interface BarcodeFinderCallback {
        void onNoScanClicked();

        void onNoBarcodeClicked();

        void finishActivity();

        void setActivityResult(Bundle bundle);

        void showProgressDialog(boolean showProgress);
    }

    private class CameraHandlerThread extends HandlerThread {

        Handler mHandler = null;

        public CameraHandlerThread(String name) {
            super(name);
            callBack.showProgressDialog(true);
            start();

            mHandler = new Handler(getLooper());
        }

        synchronized void notifyCameraOpened() {
            notify();
        }

        void openCamera() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    getCamera();
                    notifyCameraOpened();
                }
            });

            try {
                wait();
            } catch (InterruptedException e) {
                Log.d("BarcodeFinderFragment", "wait was interrupted");
            }
        }
    }
}

更新

感谢MeetTitan,通过将所有内容保存在后台线程中并在需要时发布到UI,我能够非常顺利地完成此工作。以下是将来可能需要它的任何人的工作代码:)

public class BarcodeFinderFragment extends Fragment implements View.OnClickListener {

    private static final String CAMERA_THREAD_NAME = "CAMERA_THREAD_NAME";
    private Camera mCamera;
    private CamViewFinder mPreview;
    private Handler autoFocusHandler;
    private FrameLayout preview;
    private ImageScanner scanner;
    private boolean previewing = true;
    private CameraHandlerThread mThread = null;

    private BarcodeFinderCallback callBack;
    private BarcodeFinderCallback dummyCallback = new BarcodeFinderCallback() {
        @Override
        public void onNoScanClicked() {
        }

        @Override
        public void onNoBarcodeClicked() {
        }

        @Override
        public void finishActivity() {
        }

        @Override
        public void setActivityResult(int resultCode, Bundle bundle) {
        }

        @Override
        public void showProgressDialog(boolean showProgress) {
        }
    };

    public static BarcodeFinderFragment newInstance() {
        return new BarcodeFinderFragment();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            callBack = (BarcodeFinderActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement BarcodeFinderCallback");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_barcode_finder, container, false);

        Button noScan = (Button) view.findViewById(R.id.btnNoScan);
        Button noBarcode = (Button) view.findViewById(R.id.btnNobarcode);
        preview = (FrameLayout) view.findViewById(R.id.cameraPreview);
        noScan.setOnClickListener(this);
        noBarcode.setOnClickListener(this);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        autoFocusHandler = new Handler();

        //Instance barcode scanner
        scanner = new ImageScanner();
        scanner.setConfig(0, Config.X_DENSITY, 3);
        scanner.setConfig(0, Config.Y_DENSITY, 3);

        callBack.showProgressDialog(true);
        openCamera();

    }

    private void openCamera() {
        if (mThread == null) {
            try {
                mThread = new CameraHandlerThread(CAMERA_THREAD_NAME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        synchronized (mThread) {
            mThread.openCamera();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        releaseCamera();

        if (mThread != null && mThread.isAlive())
            mThread.interrupt();
    }

    @Override
    public void onDetach() {
        super.onDetach();

        callBack = dummyCallback;
    }

    private void releaseCamera() {
        if (mCamera != null) {
            previewing = false;
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }

        callBack.finishActivity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnNoScan:
                callBack.onNoScanClicked();
                break;

            case R.id.btnNobarcode:
                callBack.onNoBarcodeClicked();
                break;
        }
    }

    public interface BarcodeFinderCallback {
        void onNoScanClicked();

        void onNoBarcodeClicked();

        void finishActivity();

        void setActivityResult(int resultCode, Bundle bundle);

        void showProgressDialog(boolean showProgress);
    }

    private class CameraHandlerThread extends HandlerThread implements Camera.AutoFocusCallback, Camera.PreviewCallback {

        Handler mHandler = null;

        public CameraHandlerThread(String name) throws InterruptedException {
            super(name);
            callBack.showProgressDialog(true);
            start();

            mHandler = new Handler(getLooper());
        }

        void openCamera() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mCamera = null;
                    try {
                        mCamera = Camera.open();
                    } catch (final Exception e) {
                        Log.d("BarcodeFinderFragment", e.toString());
                        callBack.setActivityResult(Activity.RESULT_CANCELED, null);
                        interrupt();
                    }
                    notifyCameraOpened();

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mPreview = new CamViewFinder(getActivity(), mCamera, CameraHandlerThread.this, CameraHandlerThread.this);
                            preview.addView(mPreview);

                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    callBack.showProgressDialog(false);
                                }
                            }, 500);
                        }
                    });
                }
            });
        }

        synchronized void notifyCameraOpened() {
            notify();

        }

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            autoFocusHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (previewing) {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mCamera.autoFocus(CameraHandlerThread.this);
                            }
                        });
                    }
                }
            }, 1000);
        }

        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {

            final Camera.Parameters parameters = camera.getParameters();
            final Camera.Size size = parameters.getPreviewSize();

            final Image barcode = new Image(size.width, size.height, "Y800");
            barcode.setData(data);

            final int result = scanner.scanImage(barcode);

            if (result != 0) {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();

                final SymbolSet syms = scanner.getResults();
                for (final Symbol sym : syms) {
                    final Bundle bundle = new Bundle();
                    bundle.putString("result", sym.getData());
                    bundle.putString("codeType", "" + sym.getType());

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            callBack.setActivityResult(Activity.RESULT_OK, bundle);
                        }
                    });
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:5)

你能否继续你的线程并使用yourContext.runOnUiThread()方法调用ui命令组?然后,您可以显示任何阻止代码,等待相机准备就绪,并从后台线程更新ui。

例如:

private class CameraHandlerThread extends ... {
    public void run() {
        getCamera();
        yourContext.runOnUiThread(new Runnable(){
            public void run()
            {
                ...
            }
        });
    }
}

然后你可以简单地new CameraHandlerThread().start();