Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use camera live feed as a activity background?

I am trying to make an App in which I need to use camera live feed as a background. (I know its a stupid thing to do but can't help, its a client demand).

I have tried doing it using SurfaceView but no success so far.

So far whatever I found on Stack Overflow are more of a guesses or directions how to do it but no real time examples or code help is out there. It would be great if someone who has done this before share a piece of code with Stack Overflow users like me.

like image 931
Varundroid Avatar asked Jul 12 '13 12:07

Varundroid


People also ask

Can camera app run in background?

On Android 9 (API level 28) and later, apps running in the background cannot access the camera . Therefore, you should use the camera either when your app is in the foreground or as part of a foreground service .

How do you open camera through intent and display captured image?

This is done as follows: Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use the onActivityResult() method to get the result, here is the captured image.

Which method of the camera class display the preview of the image captured?

SurfaceView. This class is used to present a live camera preview to the user. MediaRecorder.


2 Answers

Take a look here:

http://developer.android.com/guide/topics/media/camera.html

There is all the code you need to make an activity which shows the camera preview.

like image 51
type-a1pha Avatar answered Sep 18 '22 07:09

type-a1pha


package com.example.CameraPreview;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraView(Context context) {
        super(context);
        if(checkCameraHardware(context))  {
            mCamera = getCameraInstance();
        }
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public CameraView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if(checkCameraHardware(context))  {
            mCamera = getCameraInstance();
        }
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d("CameraView", "Error setting camera preview: " + e.getMessage());
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {

        if (mHolder.getSurface() == null){
            return;
        }

        try {
            mCamera.stopPreview();
        } catch (Exception e){

        }
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d("CameraView", "Error starting camera preview: " + e.getMessage());
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }

    private boolean checkCameraHardware(Context context) {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            return true;
        } else {
            return false;
        }
    }

    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open();
        }
        catch (Exception e){
        }
        return c;
    }

}
like image 44
Varundroid Avatar answered Sep 17 '22 07:09

Varundroid