12

I am trying to create a surface view for a camera so it renders on the surface whenever is in the view of the camera. At the moment all I can see on my camera view is a black screen view. I have tried to look on Google and here but so far I haven't found what I am looking for. Anyone can suggest me some idea.

2 Answers 2

20

I have written a class that can help you.

 public class Preview_can_work extends Activity { private SurfaceView surface_view; private Camera mCamera; SurfaceHolder.Callback sh_ob = null; SurfaceHolder surface_holder = null; SurfaceHolder.Callback sh_callback = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); surface_view = new SurfaceView(getApplicationContext()); addContentView(surface_view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); if (surface_holder == null) { surface_holder = surface_view.getHolder(); } sh_callback = my_callback(); surface_holder.addCallback(sh_callback); } SurfaceHolder.Callback my_callback() { SurfaceHolder.Callback ob1 = new SurfaceHolder.Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } @Override public void surfaceCreated(SurfaceHolder holder) { mCamera = Camera.open(); try { mCamera.setPreviewDisplay(holder); } catch (IOException exception) { mCamera.release(); mCamera = null; } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { mCamera.startPreview(); } }; return ob1; } } 

in your manifest file copy this code for camera permission

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

Explanation:

SurfaceView is a type of View which contains a SurfaceHolder. SurfaceHolder holds the surface on which we can display our media (generally frames).

mCamera is a Camera object which will contains the camera instance.

When you want to hold default Camera instance then you can simply call Camera.open();

Camera mCamera = Camera.open(); 

Now you have an open camera or you are having default camera instance. Now you need to capture frames from the camera and display it on a surface. But you cannot display it without any

surface. Here the surfaceView provides surfaceHolder and surfaceHolder provides surface to display camera frames. Now when surface will be created three callback functions will be

called.

1. public void surfaceCreated(SurfaceHolder holder) 2. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 3. public void surfaceDestroyed(SurfaceHolder holder) 

Note:- Surface will be destroyed when your application will go on pause.

surfaceCreated: surfaceCreated is a callback function which will be called when your surface will be created. In this, you can open your camera and set other attributes.

surfaceChanged: This will be called atleast one time when your surface will be created. After that it will be called whenever your surface will change(In device rotation). Here you can

start your preview because your surface have already created.

surfaceDestroyed: This will be called every time when your surface will destroy. Now if you dont have surface then where you can display you camera frames so I have released camera by using

mCamera.release(). This is very important because if your activity will be on pause and any other activity tries to open camera then it will not able to open it as you have

already open camera. Camera is a shared resource so one time only one application can use it. So remember one thing whenever you open a camera then always release it.

stopPreview: When you start preview then your camera starts capturing your frames and display it on a surface. Now if your surface have destroyed then you need to stop capturing frames

from camera so you have to call mCamera.stopPreview.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Bharat, I will def give this a go! thanks again. Does anyone have know a good article where I can read about “Camera does not work when running in emulator”? Is it true still?
ya actually while creating emulator you need to add camera in it then I think it will work. i will try and let you know..
Okay, so we do not need to have a device to use the camera to see live view? Cos some of the article I read saying we cannot run camera in emulator but I am not sure if this has been solved. Please let me know. Thanks.
@Override public void surfaceDestroyed(SurfaceHolder holder) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } Can you tell me what the above method for? Do we need to use the release() as I am not sure what that release() function for?
@BharatSharma:Thanks For the Code It Works But Camera View Shows Rottated Image(Up Down)....Hwlp Mw Please
|
0

Make shure you added the permission :

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

Also these window properties:

getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

Post some code if that doesn't work in order to help you

1 Comment

Thanks, I have already added the permission as you suggested; and I was thinking about using surfaceChanged ? At the moment all I can see on my camera perview surface is black.....I don't understand why.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.