3

i just wanna try to Access my Camera on Android. I dont know how i can get error logs, but i get an error when i call the function Camera.open().

Im using the SDK example Code, but it didnt works.

I also tried out to set mCamera to null before releasing. And yes i have setted the permission in the manifest.xml

it didnt works with the emulator even with my htc

package com.example.android.apis.graphics;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import java.io.IOException;


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);
}

}


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();
    try {
       mCamera.setPreviewDisplay(holder);
    } catch (IOException exception) {
        mCamera.release();
        mCamera = null;
        // TODO: add more exception handling logic here
    }
}

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.release();
    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 = mCamera.getParameters();
    parameters.setPreviewSize(w, h);
    mCamera.setParameters(parameters);
    mCamera.startPreview();
}

}
c0nstan
  • 53
  • 1
  • 1
  • 6
  • Post the error from the Log. To get the errors, either use the logcat in Eclipse or start logcat from the terminal/command line – LuxuryMode Nov 24 '11 at 21:13

5 Answers5

4

Have a look at the code provided in the accepted answer here. It should help get you started.

Edit: Ah, your logcat error suggests that you don't have one of the following lines in your manifest file (below the end </application> element:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
Community
  • 1
  • 1
John J Smith
  • 11,435
  • 9
  • 53
  • 72
  • it didnt helped. i still get the error when i call Camera.open(). dunno why :( – c0nstan Nov 24 '11 at 22:03
  • this is the logcat error 11-24 23:25:05.407: ERROR/CameraService(1233): Permission Denial: can't use the camera pid=29940, uid=10122 – c0nstan Nov 24 '11 at 22:25
  • 1
    i have set the permission, but inside the application tag. it must be outside. Now it works and with the android:screenOrientation="Landscape" i solved the rotation error. The Picture is now in Portrait mode, but it is compressed... – c0nstan Nov 25 '11 at 10:07
  • That sounds like a new question. If this answered your original problem, please mark it as accepted - it will help your acceptance rating. – John J Smith Nov 25 '11 at 11:09
2

It sounds like you still have the camera open or not released from a previous run or test of your code.

Maybe you stopped the execution of the code before it reached the SurfaceDestroyed event and the...

mCamera.stopPreview();
mCamera.release();
mCamera = null;

...was not called.

I had the same problem and that was the cause. You can confirm it because when it happens try to open the camera app on your phone and you will get the same error.

I adapted the same code from the http://www.41post.com/3794/programming/android-take-a-picture-without-displaying-a-preview tutorial.

I changed the

public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)

call to an "on_click" event for the SurfaceView.

public void TakePicture_click(View view)

That way I could decide when to take the photo instead of taking it as soon as the Activity opened.

Then after I Loaded the ImageView I released the camera straight away. Then I didn't get the same issue again.

I also did a check to make sure the camera was available before going back into the on_click code otherwise you'll get another error.

If anyone is interested I used this camera code to do some animation over the top of the captured image. So then had to make sure that the imageVeiw i was using was brought to the top otheriwse I couldn't see it. I found that you can't see through the SurfaceView layer.

public void TakePicture_click(View view)
{
      //check here if the camera is still running in case you click the screen later
      //otherwise you'll get an error.
    if (mCamera !=null) 
    {
            //sets what code should be executed after the picture is taken

        Camera.PictureCallback mCall = new Camera.PictureCallback()
        {
            @Override
            public void onPictureTaken(byte[] data, Camera camera)
            {
                //play the shutter.wav sound
                mPlay.start();

                //decode the data obtained by the camera into a Bitmap
                bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

                //store the image the imageView
                background_image.setImageBitmap(bmp);

                //I use another imageview to show a layer over the captured image 
                //so you have to bring the imageview to the top...
                //because you can't see through the SurfaceView. 
                animate_image.bringToFront();

                //release the camera straight away so we don't have any chance of it staying open
                if (mCamera !=null) {
                    //stop the preview
                    mCamera.stopPreview();
                    //release the camera
                    mCamera.release();
                    //unbind the camera from this object
                    mCamera = null;
                }

            }
        };

        mCamera.takePicture(null, null, mCall);
    }
}

Hope that helps you and anyone else who has the same problem.

Thanks to DimasTheDriver for the original tutorial it helped me out at the time as I wanted a simple capture routine without the camera controls and confirmation screen.

Shutter Sound

For those who are interested I also added a shutter click sound which can be found at: wwww.freesound.org/people/Nathan_Lomeli/sounds/79190 Put the .wav file in your res/raw folder

Define the player for the .wav file before OnCreate

private MediaPlayer mPlay

Create the player for the .wav file in OnCreate

mPlay = MediaPlayer.create(this, R.raw.shutter);

Play the shutter.wav sound in the On_Click when you take the picture

mPlay.start();
birdman
  • 1,134
  • 13
  • 13
  • For those who are interested I also added a shutter click sound. found at:
    http://www.freesound.org/people/Nathan_Lomeli/sounds/79190/

    Put a .wav file in your res/raw folder.

    //define the player for the .wav file before OnCreate
    private MediaPlayer mPlay ;

    //Create the player for the .wav file in OnCreate
    mPlay = MediaPlayer.create(this, R.raw.shutter);

    //play the shutter.wav sound in the On_Click when you tak e the picture
    mPlay.start();

    – birdman Nov 28 '12 at 09:45
1

Place permissions above the Application tag, in your manifest file

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

 <application>
          <activity></activity>
 </application> 

i kept the permissions on wrong place thats why i kept on getting the same error! Hope this will help you too

Fazal
  • 3,374
  • 1
  • 15
  • 20
0

In my case the problem was caused by missing flashlight permission:

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

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
0

if you already place permissions above <application> element in AndroidManifest and it not help, check your import, it should be import android.hardware.Camera

doris4730
  • 11
  • 1
  • 2