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
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
.

- 1,711
- 3
- 13
- 23

- 3,926
- 2
- 17
- 29
-
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? – Kitaro May 07 '12 at 13:49
-
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.. – Bharat Sharma May 07 '12 at 13:55
-
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. – Kitaro May 07 '12 at 13:58
-
1`@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? – Kitaro May 07 '12 at 14:09
-
@BharatSharma:Thanks For the Code It Works But Camera View Shows Rottated Image(Up Down)....Hwlp Mw Please – Erma Isabel Oct 10 '13 at 09:22
-
@Kitaro to set your camera view you need to call setRequestedOrientation() or setRotation according to your api level. These functions takes different parameters for camera view rotation. you can use according to your requirement. – Bharat Sharma Oct 10 '13 at 10:08
-
@BharatSharma can u pls help me how can i display camera obj in two surface holders .... i have added my surfaceHolder in Framelayout its working fine but i want to show same camera obj live camera preview in two different framelayout adding it in second framelayout giving me error that cameraInstance has already its parent view . – Erum Oct 18 '14 at 14:38
-
@ErumHannan According to me you can create a new surface holder and add it to your frame layout. Problem you cannot use your camera at two difference places at the same time so what you can do is you can create another surface holder callback and add it to your surface holder. I did not try it but you can give it a try. If its not working then simply copy frames from your previous surface to another. – Bharat Sharma Nov 23 '14 at 12:35
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

- 11,557
- 14
- 94
- 187
-
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. – Kitaro May 07 '12 at 12:43