block Until Open Camera - Android Camera

Android examples for Camera:Open Camera

Description

block Until Open Camera

Demo Code

/*//from  www .  j  a  v a 2s. com
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.os.Handler;
import android.util.Log;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class Main{
    /**
     * @return true if success to open camera, false otherwise.
     */
    public static boolean blockUntilOpenCamera(CameraManager cameraManager,
            Handler handler) {
        try {
            String[] cameraIdList = cameraManager.getCameraIdList();
            if (cameraIdList == null || cameraIdList.length == 0) {
                return false;
            }
            String cameraId = cameraIdList[0];
            CameraCallback callback = new CameraCallback();
            cameraManager.openCamera(cameraId, callback, handler);
            return callback.waitForResult();
        } catch (Exception ex) {
            // No matter what is going wrong, it means fail to open camera.
            ex.printStackTrace();
            return false;
        }
    }
}

Related Tutorials