Ask Your Question

dmartel's profile - activity

2016-04-21 16:31:19 -0500 received badge  Popular Question (source)
2014-02-26 15:19:04 -0500 received badge  Student (source)
2014-01-15 16:10:44 -0500 asked a question Why does using VideoCapture on android when the screen is off causes the camera to lock until rebooted?

I am using OpenCV 2.4.8 and I am trying to make an Android Application that uses the camera in the background when the screen is off. However, when ever I try to use VideoCapture in a Service, the camera becomes locked and the only way to recover is to reboot your device. To reproduce this problem, I created a dummy service that takes 200 frames every 5 minutes and does nothing with them. If I run the Service with the screen on, it works fine. If the phone is plugged into my computer and I am monitoring logcat, it does not lock even if the screen is off. However if I run it with the screen off and not debugging on my computer, the camera will eventually freeze or the phone with reboot. This is true if I replace VideoCapture with android.hardware.Camera too. Is there something in Android or the camera drivers that prevents the camera from being used if the screen is off?

I've tried it on the following devices and it locks

RAZR M (this one locks the fastest) Nexus 7 Gen 2 Samsung Galaxy Note 3 Asus Transformer TF700

It does not lock on Samsung Galaxy S3 SGH-T999 TMobile with 4.3, but it did before the 4.3 update

Below is the logcat for OpenCV with a bad camera get

OpenCV_NativeCamera: Connecting to CameraService v 2.3 OpenCV_NativeCamera: initCameraConnect: Unable to connect to CameraService OpenCV::camera: CameraWrapperConnector::connectWrapper ERROR: the initializing function returned false OpenCV::camera: Native_camera returned opening error: 6



import java.util.Calendar;

import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;

import com.ImageInsightInc.GammaPixServiceTest.R;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class DummyService extends Service
{
    // might need a way to determine who is telling us what to do
    public static final String BUNDLE_TAG_SOURCE    = "source";
    public static final String SOURCE_ALARM_MANAGER = "AlarmManager";
    public static final int ALARM_PERIOD_IN_MINUTES = 1;    
    public static final int ALARM_PERIOD_IN_MSEC = ALARM_PERIOD_IN_MINUTES*60*1000;


    static 
    {
        if (!OpenCVLoader.initDebug()) 
        {
            Log("OpenCV Load Failure");
        }
        else
        {
            Log("OpenCV Load success");
        }
    }    

    // gets invoked once - gets invoked via StartServiceBroadcastReceiver when the phone is started
    @Override
    public void onCreate() 
    {   
        Log("DummyService: onCreate");

        setupAlarmManager();

           // Start foreground service to avoid unexpected kill
        Notification notification = new Notification.Builder(this)
            .setContentTitle("Background Video Recorder")
            .setContentText("")
            .setSmallIcon(R.drawable.ic_launcher)
            .build();
        startForeground(1234, notification);

    }


    // gets invoked each time startService is called or via alarm manage
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {   
        final int NumberOfFrames = 200;
        int pictureCount = NumberOfFrames;      
        VideoCapture mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);

        if(mCamera.isOpened())
        {
            int width = 1920;
            int height = 1080;
            mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, width);
            mCamera.set(Highgui ...
(more)