What is this: I was working on the CameraPreview application and noticed there were no capturing tool to save the image into a PNG or JPG. It is quite simple to enable it but requires a bit of understanding.

Problems/Questions: post right below...

Difficulty: 2.5 of 5

Desctription: First off, we create a new project within Eclipse. I named it CameraPreview so I can just copy/paste the cameraPreview application found here.

If you run the application, you should see a square changing color with a black and white background:

Let analyse how the camera application is built:

The CameraPreview does not do much. It is our main class but nothing in there is of any interest.

The Preview class is where everything happens. It will contain the canvas which we will draw onto and then to the screen. The constructor and most methods are used to handle the extra thread that we will use to keep refreshing the output of the camera. For instance we will set the size of the screen (and camera output) in the constructor, ensure that the PreviewThread will be killed when the activity goes onPause and reborn when it hits onResume.

The surfaceView is just a handler on which we will be able to paint the output of the camera.

It is important to understand how the preview is done before continuing in the capturing part. as you can see we lock onto a canvas that is being updated by the camera. The camera can be open only once.

I used the center button to be the one that will take the picture. First I pause the Preview class. The onPause will kill the the thread which is looping as explained above and close the connection to the camera device. This will release the device for any other method to use. The takePicture is the method used to take the picture - I will explain it next. The resume will call the onResume so we can have the preview continuing after the picture was taken.

4. Now I open a file using Context.openFileOutput
NOTE: As you can see I use a dynamic name. I noticed that overiding the file with the same name will not work. It will recreate a new file but it seems that you can not open the png afterwards. It might be a bug, need time to investigate a bit further. Now each time you take a picture it will be named picture1.png, picture2.png and so forth. Furthermore, you might want to wait a bit and refreshing the ddms view before saving...

5. I capture the image onto the canvas (onto the bitmap) and then save it by compressing it onto the outputStream:

Nothing much to explain here. It is quite straightforward to understand. You have 2 formats: PNG and JPG. The second value is the quality 100 being best and 0 worth. FInally stream is the ouptut stream that were given in 4.

Next I will create a quick viewer. However, you can check the files from:

Open Perspective -> DDMS -> under "data -> data -> <package name> -> files -> picture0.png...". You can save it onto disk with the small disk drive icon on the top right. and check how marvelous the picture is.

I attached the source code of the application. You can import it directly into eclipse and run it. I think the problem relates, as plusminus says, to the scope. openFileOutput is a method taken from the class Context which is being extended by Activity. so instead of cp.openFileOutput("picture" + i++ + ".png", 1); you can write (or should be able to) this.openFileOutput.

Anyway it might be better if you check the source and compare it to your code.

Hi charroch,
Can you tell me how to draw Image or anything on the Camera Preview. While taking picture that picture should be combination of image and Camera Preview. Do u have any idea about this problem.

You can call me carl btw . I am on vacation for the next week and I don't have my plateform at hand. However you should be able to use the canvas class to draw something on top of the current image. I guess you can capture an image and then draw whatever you want. If you want to draw an image it might be a bit more difficult. You could use a BitmapDrawable or Bitmap and then setBound function and draw it onto the canvas.

Hi,
Thanks for ur code carl its work nice.. But im finding some problems whatever i do its not getting install on my simulator.. Only during the launch time it works if i close it and try to locate there is no such application ... I even tried using adb install. Same problem it shows the installation size but no use.

And one more thing i could not close the application the only way to exit is using the Home Button.