13

I'm currently developing an app which need to record video in background and process it.

(It needs to get camera preview data real-time in background and have to image process the preview data)

However, to achieve it, I need to use Camera and OpenCV as Service, and it seems that it is impossible to use JavaCameraView in OpenCV and Android.Hardware.Camera without using any preview.

Here are my questions.

  1. I heard that NativeCamera in OpenCV can be used for this purpose. Is it possible? (Possibly with examples?)

  2. Is there any method that I can use JavaCameraView(or similar stuff) for this purpose? I currently use Galaxy S4.

  3. Is there any possible workarounds if android doesn't support such method?(Using Camera Preview without any surface view, or Process camera data without using preview)

  4. (OPTIONAL)Why the android doesn't support such operation? It is very annoying!

Thank you for answering the question.

EngineerCat
  • 161
  • 1
  • 1
  • 5
  • I have the same situation like you now. Have you got some solution? – Peter Zhu Mar 19 '15 at 02:11
  • Possible duplicate of [android - use camera from within background service](http://stackoverflow.com/questions/6901542/android-use-camera-from-within-background-service) – Alex Cohn Nov 06 '15 at 13:54
  • 1
    https://github.com/kevalpatel2106/android-hidden-camera - this library provides easy interface to capture image from the background. – Keval Patel Dec 09 '16 at 16:25

2 Answers2

1

Yes it is possible with following steps..

Create one activity which will start your background service on some event or you can also use alarm manager to start and stop the service as per your requirement.

See the below code that'll help you.

public boolean starMediaRecording(){
        Camera.Parameters params = mServiceCamera.getParameters();
        mServiceCamera.setParameters(params);
        Camera.Parameters p = mServiceCamera.getParameters();

        final List<Size> listSize = p.getSupportedPreviewSizes();
        Size mPreviewSize = listSize.get(2);
        p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        p.setPreviewFormat(PixelFormat.YCbCr_420_SP);
        mServiceCamera.setParameters(p);

        try {
            mServiceCamera.setPreviewDisplay(mSurfaceHolder);
            mServiceCamera.startPreview();
        }
        catch (IOException e) {
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }

        mServiceCamera.unlock();

        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setCamera(mServiceCamera);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        mMediaRecorder.setOutputFile("/sdcard/filenamevideo.mp4");
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
        mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

        mMediaRecorder.prepare();
        mMediaRecorder.start(); 

        mRecordingStatus = true;

        return true;

}

public void stopMediaRecorder() {
    mServiceCamera.reconnect();

    mMediaRecorder.stop();
    mMediaRecorder.reset();

    mServiceCamera.stopPreview();
    mMediaRecorder.release();

    mServiceCamera.release();
    mServiceCamera = null;
    }
}
Bhaumik Thakkar
  • 580
  • 1
  • 9
  • 28
  • 2
    Downvoted because you literally copied and pasted the answer from another StackOverflow post and didn't even bother to credit the original author: https://stackoverflow.com/a/14997460/596841 – pookie Jul 21 '20 at 12:19
0

You can use a service to start your camera in background. You can refer to this . Hope this will helps you.

Community
  • 1
  • 1
RussVirtuoso
  • 900
  • 1
  • 9
  • 20
  • The problem is that the commonly used method for implementing camera in Service doesn't work. – EngineerCat Oct 10 '13 at 07:55
  • Many people suggested using Dummy Surfaceview, or make surfaceview virtually invisible(by making it very very small), but the dummy surfaceview failed for my device. Also, the surfaceview cannot be used because I need to develop such that the camera should work even if I surf through other apps(cause I want to apply it for motion-based UI purpose), and this will cause the surfaceview(regardless of size) to be destroyed. – EngineerCat Oct 10 '13 at 07:58