0

I am trying to open camera applicaiton using following code:

 Camera camera = Camera.open();
 Parameters p = camera.getParameters();
 p.setFlashMode(Parameters.FLASH_MODE_ON);
 camera.setParameters(p);
 camera.startPreview();        
 camera.release();

It does not throw any error but it does not open the camera either. I tried it both with and without camera.release() option. Is there anything that I am doing wrong?

Lost
  • 12,007
  • 32
  • 121
  • 193
  • Are you trying to open the seperate camera application? Or open camera view within your own application? – frogmanx Oct 29 '13 at 22:52
  • All I want is to open camera. It can open saperate camera application since I do not need any call back or control back to my application. Do I need to go fancy by opening Surface view on this? – Lost Oct 29 '13 at 23:04
  • Nope, I'll change my answer to align with that – frogmanx Oct 29 '13 at 23:06

1 Answers1

4

Make sure relevant permissions exist in your manifest.

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

According to google Dev:

Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.

Make sure your Activity implements SurfaceHolder.Callback

Camera camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(p);
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView1); //add this to your xml view
SurfaceHolder surfaceHolder = surfaceView.getHolder()
surfaceHolder.addCallback(this);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();      

Alternatively, if you just want to open any Camera application, use:

Intent newCameraApp = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(newCameraApp , 1337);
frogmanx
  • 2,620
  • 1
  • 18
  • 20
  • Thank you so much for your response. Actually I just need to open camera app. and your secind solution sounds much better BUT I also want to set the parameters like flash and resolution and somehow when I do it using intent it does not set the flash paramters. I also posted a question on SO here:http://stackoverflow.com/questions/19667094/intent-does-not-set-the-camera-parameters. If I can get answer to that. i dont need to open Camera object and can achieve that with simplicity. – Lost Oct 29 '13 at 23:08
  • I thought you did put some surface code here. I think i am doing it with surfaceView now and your code might had helped.:)) – Lost Oct 29 '13 at 23:56
  • Reverted to the previous one :) – frogmanx Oct 29 '13 at 23:59
  • By the way, I am seeing all the examples of surface view that can just hold the camera as it is without any changes in the funcitonality. I am looking at different examples but seems like they are trying to do too many things. Can I not create a blank surfaceView and add camera into it? I have opened up a new question if you can help me? http://stackoverflow.com/questions/19671297/open-camera-in-a-blank-surfaceview – Lost Oct 30 '13 at 00:05
  • I was wondering if you could explain Camera.open(). What would cause it to return null? I am trying to figure out why in my application it always return ms null, and I knownI have the correct permissions set, so I thought it might have had to do with the preview possibly not being ready yet. But from your code, the preview is added to the camera afterwards. Is it really afterwords, or am I not understanding the flow of a callback? – Acoop Aug 14 '15 at 02:02
  • @user3650087 What device are you using in particular? You could also try using Camera.open(0) to get the first camera, as Camera.open() only gets the backwards facing camera or returns null otherwise. – frogmanx Aug 17 '15 at 00:58
  • I am using Google Glass. The preview surface which is causing the trouble is exactly the same code that works fine when in its own activity, but posing issues when embedded in a cardBuilder Embed style card. – Acoop Aug 17 '15 at 02:11