coler'e
coler'e

Reputation: 61

Camera preview via android application

In order to preview the camera, I've written code segments below. I could not get any preview. Can you help me to solve the problem?

I have configured my application with the permissions ;

    <uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

I have created the surface place

<SurfaceView
    android:id="@+id/cameraView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

I've written code segments below to preview the camera ;

public class MainActivity extends Activity  {

SurfaceView surfaceView;
SurfaceHolder surfaceHolder;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.cameraView);
        surfaceHolder = surfaceView.getHolder();
}

 private void preview ( ) {

    try {
             camera.setPreviewDisplay(surfaceHolder);
             camera.startPreview();
    } catch ( IOException void_) {
        ;
    }
}

Upvotes: 0

Views: 113

Answers (3)

swiftBoy
swiftBoy

Reputation: 35783

You can capture Image from camera using..

  1. Intent (open default camera and capture image)
  2. Camera API (get the image preview, also you can customize camera feature)

Please read this article step by step solution, I found this is very helpful

Android Camera API - Tutorial

Let me know if you have any trouble in this, I have done already

Edit:

You can disable Camera using Admin Policy

check official document here with sample code also this article may help you

Upvotes: 1

Priya
Priya

Reputation: 1803

implement this class for camera preview and called its constructor in oncreate method of your activity.

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mSurfaceHolder;
    private Camera mCamera;

    @SuppressWarnings("deprecation")
    public CameraPreview(Context context, Camera camera) {
        super(context);
        this.mCamera = camera;
        this.mSurfaceHolder = this.getHolder();
        this.mSurfaceHolder.addCallback(this);
        this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        try {
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();
        } catch (IOException e) {
            // left blank for now
        }
    }


    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mCamera.stopPreview();
        mCamera.release();
    }


    public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
            int width, int height) {
        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();
        } catch (Exception e) {
            // intentionally left blank for a test
        }
    }
}

Upvotes: 0

AndroidLearner
AndroidLearner

Reputation: 4636

Before the 4.0,The Android SDK provided by Google doesn't provide any camera emulation beyond a stand-in animation. Since Google haven't yet provided a working implementation (or even a date by which one will be available).

There is certain links which helps you to achieve your requirement. Kindly refer this and this links.

Hope it may help you :)

Upvotes: 0

Related Questions