opc0de
opc0de

Reputation: 11768

Draw on SurfaceView while camera is on

I have the following code for accessing the camera preview i want to display some text in the upper corner of the preview picture how can i do this ?

package com.gps.ro.capturetest;

import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class CaptureTestActivity extends Activity implements SurfaceHolder.Callback {
         SurfaceHolder mSurfaceHolder;
        SurfaceView mSurfaceView;
        public Camera mCamera;
        boolean mPreviewRunning;
        Canvas test;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.main);
        mSurfaceView = (SurfaceView) findViewById(R.id.camera_surface);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);




    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int h, int w) {
        // TODO Auto-generated method stub
        if (mPreviewRunning) {
                            mCamera.stopPreview();
                        }
                        Camera.Parameters p = mCamera.getParameters();
                        mCamera.setDisplayOrientation(90);
                        p.setPreviewSize(h, w);
                        p.set("orientation", "portrait");

                        //p.setPreviewSize(w, h);
                        mCamera.setParameters(p);
                        try {
                            mCamera.setPreviewDisplay(arg0);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        mCamera.startPreview();
                        mPreviewRunning = true;

    }

    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        mCamera = Camera.open();
    }

    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
                mCamera.stopPreview();
                mPreviewRunning = false;
                mCamera.release();
    }

    public void Trigger(View V)
    {
        ShutterCallback shutterCallback = new ShutterCallback() {
            public void onShutter() {
                Log.d("DHA", "onShutter'd");
            }
        };

        PictureCallback rawCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                Log.d("DHA", "onPictureTaken - raw");
            }


        };
        Log.w("DHA", "Intra aici!");
        mCamera.takePicture(shutterCallback, rawCallback, null);

    }







}

Upvotes: 2

Views: 1632

Answers (1)

yoah
yoah

Reputation: 7230

Create another view that is on top of the surface view, and draw your decorations in that view. For example, you could use a FrameLayout and in it the SurfaceView followed by the view you want on top of the preview.

Upvotes: 2

Related Questions