Android Tutorial Take pictures with the internal camera

Status
Not open for further replies.
It is recommend to use the new CameraEx class. It provides more functionality.
The new Camera library allows you to take pictures using the back facing camera.

The following steps are required for taking a picture:
- Initialize a camera object. The Initialize method expects a Panel. The preview images will be displayed on this panel.
- Wait for the Ready event.
- In the Ready event Sub, call Camera.StartPreview to show the images preview.
- Call TakePicture to take a picture.
- The PictureTaken event will be raised with the picture passed as a bytes array (in JPEG format).
- Call StartPreview again to restart the images preview.

Only one process can access the camera at a time. Therefore you should release the camera when your activity is paused.
Usually you will initialize the camera during Activity_Resume and pause it during Activity_Pause.

camera_1.png


The following code saves the image:
B4X:
Sub Camera1_PictureTaken (Data() As Byte)
    Dim out As OutputStream
    out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
    out.WriteBytes(data, 0, data.Length)
    out.Close
    ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "1.jpg"), True)
You can see in the attached code that the "take picture" button is only enabled when the camera is ready.

For now the camera orientation is always set to landscape mode. Usually you will want to force the activity to also be in landscape mode. This is done by checking 'Lansdcape' in Project - Orientations Supported menu.

On the emulator the preview images will show a moving check board.
 

Attachments

  • CameraExample.zip
    5.7 KB · Views: 9,508

appie21

Active Member
Licensed User
Hi

i have load the camera and i can take a picture, but if i will show the picture on a imageview it goes wrong the second time

i want if i take a picture directly load on to a image view.
When i take the first time a pictue it is ok but the seccond timme the application stopped

see


B4X:
Sub Camera1_PictureTaken (Data() As Byte)
   camera1.StartPreview
   Dim out As OutputStream
   out = File.OpenOutput(File.DirInternalCache, "1.jpg", False)
   out.WriteBytes(Data, 0, Data.Length)
   out.Close
   ToastMessageShow("Image saved: " & File.Combine(File.DirInternalCache, "1.jpg"), True)
   btnTakePicture.Enabled = True
   ImageView1.Bitmap = LoadBitmap(File.DirInternalCache, "1.jpg")'this goes wrong second time
End Sub

what do i wrong?
 

appie21

Active Member
Licensed User
thank you
this is the right one

ImageView1.Bitmap = LoadBitmapSample(File.DirRootExternal, "Image.jpg", 100,100)
 

wheretheidivides

Active Member
Licensed User
Currently only landscape is supported (this is the native camera orientation).

When I run it, the video is stretched out across the screen. It is not a true image. Like when a SD television is stretched on a HD TV (4:3 aspect ratio to 16:9). I think it is because my phone has a longer aspect ratio. ANy ideas how to fix this?
 

Rick Harris

Well-Known Member
Licensed User
Take picture as bitmap

Regrettable the Camera1_Preview event does not trigger in my App. No idea why. It does work in the example project.

Is there a way to make the Camera take a picture in BMP format in stead of JPEG? That way it would be easy to get the RGB values of any pixel by using the Data Array of the Camera1_PictureTaken event!
 

Alberto Iglesias

Well-Known Member
Licensed User
Erel,

I try to use this library in a device with Android 2.2.1 and only Facing Camera, I use your simple example, the StartPreview works very weel, but when I push the button, the _PictureTaken Event not firing. In anothers devices with 2.35 and 4.1.2 works very well.

You know why?

I try anothers libraries, like FrontCamera (by vb1992) and not work too.

Can I get the picture capturing a frame or something like that?

the device I have to do this is: http://www.archos.com/products/home/archos_35_hc/index.html?country=us&lang=en


thank You
 

Adam888

Member
Licensed User
Hi Erel,

How can I mirror the image in preview mode? I try to use "setDisplayOrientation" , but it only rotate the image in different direction.
Thank you
 
Status
Not open for further replies.
Top