Surface View Camera Preview - android

Having a little trouble getting my SurfaceView to show my camera preview. I've looked at some questions on here and Google'd some tuts but I think it may be a small error on my end that I'm just not seeing.
Code
public class RoofPitchActivity extends Activity implements SensorEventListener {
...
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roof_pitch);
initViews();
}
private void initViews() {
...
Preview preview = new Preview(this);
mCamera = Camera.open();
preview.setCamera(mCamera);
}
...
...
class Preview extends ViewGroup implements SurfaceHolder.Callback {
public Preview(Context context) {
super(context);
mSurfaceView = (SurfaceView) findViewById(R.id.preview);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
}
public void setCamera(Camera camera) {
if (mCamera == camera) {
return;
}
stopPreviewAndFreeCamera();
mCamera = camera;
if (mCamera != null) {
requestLayout();
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}
private void stopPreviewAndFreeCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera.startPreview();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mSurfaceView.getWidth(), mSurfaceView.getHeight());
requestLayout();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
}
So when the activity is launched the SurfaceView is black. The camera does not appear to be rendering the preview onto the SurfaceView. I'm sure there's something small I'm missing, or maybe it's just a fundemental misunderstanding of how this works. A fresh set of eyes with a little explanation would be very much appreciated. Thanks

So the answer to my question ended up being device specific. The code was acceptable but my device I'm using to debug is a Nexus 7. This means the camera is front facing and the normal call to the camera does not work. An interesting and funny read about this can be found in this article
Camera.open();
does not work. You must use
Camera.open(0);
So my code ended up with a conditional check for the camera and now it works fine.
Like so
public void setCamera() {
stopPreviewAndFreeCamera();
PackageManager pm = getPackageManager();
boolean backCamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
boolean frontCamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
if (frontCamera) {
mCamera = Camera.open(0);
} else if (backCamera) {
mCamera = Camera.open();
}
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mSurfaceView.getWidth(), mSurfaceView.getHeight());
requestLayout();
try {
mCamera.setParameters(parameters);
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}

Related

Do I save battery if I set CameraPreview visibility to GONE?

I have a simple class called CameraPreview, this is the code:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open();
} catch (RuntimeException e) {}
try {
SurfaceHolder someHolder = holder;
mCamera.setPreviewDisplay(someHolder);
mCamera.startPreview();
} catch (IOException e) {
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null)
return;
try {
mCamera.stopPreview();
} catch (Exception e){}
mCamera.setDisplayOrientation(90);
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){}
}
}
It's obvious that, while the application is showing the CameraPreview on screen, it will consume battery faster, my question is,what will happen to the battery if I set this view to View.GONE?
According to this SO answer setting the SurfaceView visbility to GONE will call surfaceDestroyed method.
Therefore, since you are stopping the camera, you won't consume user's battery.

Camera flashlight on galaxy s3

I am using following code to turn on camera flashlight
public Flashlight(SurfaceView preview, Context context){
this.preview = preview;
this.context = context;
mHolder = preview.getHolder();
mHolder.addCallback(this);
mCamera = Camera.open();
cameraOpened = true;
try {
mCamera.setPreviewDisplay(mHolder);
}catch (Exception e){
e.printStackTrace();
}
params = mCamera.getParameters();
mCamera.startPreview();
}
private void turnOnFlashlight(){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
}
private void turnOffFlashlight(){
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mHolder = holder;
try {
mCamera.setPreviewDisplay(mHolder);
}catch(Exception e){
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mHolder = null;
}
it uses surface view and it works great on many phones however I cant get it to work on galaxy S3. Have you experienced similary problems? Any ideas how to get it working on S3?
Thanks in forward

Use camera but dont want to take photo

I want to use camera in my app but dont want to take photo, actually I am making a app i.e transparent screen, in this I want to show transparent wallpaper i.e I have to start camera for this and i dont want to take images for this
I tried all these codes but have n't got the desirable results. Can anyone suggest what must i do?
used this permission in all cases
<uses-permission android:name="android.permission.CAMERA"/>
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
startActivity(intent);
Intent intent = new Intent(android.provider.MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivity(intent);
Update 1:
I tried this code it showing camera in not correct way, its diverting the preview to right kindly look over this updated code and tell wht amendment i can make over this
public class MainActivity extends Activity {
private Preview mPreview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
setContentView(mPreview);
}catch(Exception e){
e.printStackTrace();
}
}
******************************************************************************************
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
try{
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}catch(Exception e){
e.printStackTrace();
}
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
try{
if(mCamera!=null){
mCamera.release();
mCamera=null;
}
mCamera = Camera.open();
Log.i("Camera", "Camera is opened");
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
try{
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}catch(Exception e){
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
try{
Parameters parameters = mCamera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}catch(Exception e){
e.printStackTrace();
}
}
}
You need to use surfaceView for this. Here is an example:
public class CameraPreview extends Activity {
private Preview mPreview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
setContentView(mPreview);
}
}
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
camera.setParameters(parameters);
mCamera.startPreview();
}
}
I used this code and it worked...
<uses-permission android:name="android.permission.CAMERA"/>
public class MainActivity extends Activity {
private Preview mPreview;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
setContentView(mPreview);
}catch(Exception e){
e.printStackTrace();
}
}
}
*****************************************************************************************
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
try {
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} catch (Exception e) {
e.printStackTrace();
}
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
try {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
mCamera = Camera.open();
Log.i("Camera", "Camera is opened");
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
try {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
try {
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}

camera preview on lockscreen

I want add a take picture function in lockscreen , and I need to open the preview on screen , but it's fail all the time , logcat say the Error is about "setPreviewWindow - Null ANativeWindow passed to setPreviewWindow" and "startPreview - Preview not started. Preview in progress flag set"
In keyguard_screen_tab_unlock.xml after the digital clock view, put
<FrameLayout
android:id="#+id/mPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
In LockScreen.java constructor:
FrameLayout mFrameLayout = (FrameLayout)findViewById(R.id.mPreview);
SurfacePreview preview = new SurfacePreview(mContext);
preview.setZOrderOnTop(true);
mFrameLayout.addView(preview);
SurfacePreview class :
public class SurfacePreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
SurfacePreview(Context context) {
super(context);
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
}
}
Anyone kown how to resolve this issue and start prview on LockScreen? Thanks a lot.

Android Moto Droid Camera Hangs EVERY Time onTakePicture

All, I've googled over and over again to find a solution and while I found a bug regarding camera release, etc I can not for the life of me seem to get the cam code to work. Every time I executed takePicture the system simply hangs, sometimes it calls the PictureCallback, but most of the time it simply hangs.
Weird issues about not being able to read /data/ap_gain.bin files, etc
Below is the code:
public class CameraActivity extends Activity implements Camera.PictureCallback, RequestConstants {
private static final String TAG = "Camera";
private Preview preview;
private boolean previewRunning;
private int addressNotificationId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addressNotificationId = getIntent().getIntExtra(REQ_RA_ID, 0);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (preview == null) {
preview = new Preview(this);
}
setContentView(preview);
}
#Override
protected void onDestroy() {
if (isFinishing()) {
preview.cleanup();
}
super.onDestroy();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CAMERA) {
/*
preview.setDrawingCacheEnabled(true);
Bitmap ss = preview.getDrawingCache();
byte[] data = ImageUtility.getImageData(ss,75,1);
Log.v(TAG, "Pic with size: " + data.length);
ApplicationManager.getInstance().createPacketRecord(PacketConstants.PT_FLAG_ADDRESS_PHOTO, ApplicationDatabaseManager.getInstance().getRouteAddressBySystemId(addressNotificationId), data);
finish();
*/
preview.getCamera().takePicture(new Camera.ShutterCallback() {
#Override
public void onShutter() {
}
}, null, this);
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onPictureTaken(byte[] data, Camera camera) {
/*
if (data == null || isFinishing())
return;
camera.stopPreview();
previewRunning = false;
camera.release();
*/
//Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);
//data = null;
//data = ImageUtility.getImageData(bitmap, 75,1);
Log.v(TAG, "Pic with size: " + data.length);
ApplicationManager.getInstance().createPacketRecord(PacketConstants.PT_FLAG_ADDRESS_PHOTO, ApplicationDatabaseManager.getInstance().getRouteAddressBySystemId(addressNotificationId), data);
finish();
}
}
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
Camera getCamera() {
return mCamera;
}
void cleanup() {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera == null)
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
parameters.setPictureSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
You are sometimes taking a picture and sometimes not, because you are releasing the camera before the callback has occurred.
When the callback does fire, depending on whether the camera has released or not it will or will not be able to access the taken photo.
I suggest making sure that you are not releasing the camera or closing the form when you take a photo.
Better yet, close the form from the photo callback.
Also, when a photo is taken, the default action of android is to stop the Preview. this is not a bug, but the expected nature.
After you JpegPictureCallback is called, you need to call mCamera.startPreview again.
There isn't enough info in the API Camera sample to really write a camera app, but you can get the source code for Google's own camera app here. Camera.java contains a lot of important, useful code:
git://android.git.kernel.org/platform/packages/apps/Camera.git

Categories

Resources