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: 10,126

Swissmade

Well-Known Member
Licensed User
Longtime User
The native Camera object is supposed to return a byte array with the complete data. It seems like a bug in your device. Try to restart your device and see if it helps.

This I have done Erel,

Strange thinks happend.
I have the compleet stream in the Array. See my other thread Sending files with TCP/IP. Sending then image to the server the I get the hole Image.
If Save it I dont get it compleet.

I try to put some DoEvents:BangHead: see whats happend.
 

Swissmade

Well-Known Member
Licensed User
Longtime User
DoEvents will not help here.
You should send the file length before sending the file itself. Then in your server you should read until you read the exact length.

I mean when i Save it to a file on the device I dont get the hole File.
No Problem I try something else.
Whats important that I get it write on the server.

Thanks for Help Erel
:icon_clap:
 
Last edited:

basvanbreg

New Member
Just what i need.

I want to see the sample but i. can't download the attachment. i don't have permission or so... help me out please:sign0085:
 

giangvi

New Member
Hello Erel,
every time I download the example, I always get this message:

"giangvi, you do not have permission to access this page. This could be due to one of several reasons:

1. Your user account may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
2. If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
3. Please read our forum permissions policy.
"

Can you show me how to solve this?
 

koezuma

Member
Licensed User
Longtime User
camera with black screean

dear erel,

i try your camera program without modification, but only black screen and take camera button appear on the device. no activity if i click take camera button. why?
i use android 1.6.

best regards
koezuma

:sign0085:
 

Swissmade

Well-Known Member
Licensed User
Longtime User
Take image from Camera

dear erel,

i try your camera program without modification, but only black screen and take camera button appear on the device. no activity if i click take camera button. why?
i use android 1.6.

best regards
koezuma

:sign0085:

hi koezuma,
If you dont know use this

Dim camera1 As Advancedcamera 'Camera
function Camera1.StartPreview

If this is not working then you have a android version problem.
I hope it helps.
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Video

Erel
I am wondering myself to be forced to use Java for grabbing videos. Do you expect to activate this facility in B4Android in some time, or it is far from your expectations?
Thanks
 

chfajardo

Member
Licensed User
Longtime User
Camera - NullPointerException on reading Data()

Hi, Erel!

I have tried your code, and it works fine on my cellphone, but in my tablet it gives me nullPointerException every time a try to take a picture.
The model of my tablet is Coby Kyros MID7024.
Do you have some advice to fix this issue?
On my cellphone, the LoadBitmap() function works sometimes, and sometimes not. What can it be?

Thanks in advance.

Carlos H. Fajardo.
 

chfajardo

Member
Licensed User
Longtime User
Camera - NullPointerException on reading Data()

Erel,

Thanks for the reply, but this happens when i am saving data() matrix.
Anyway, i found out that using AdvancedCamera instead of Camera and setting, for instance, camera1.Picturesize(600,800) works fine now.
 

andrewtheart

Member
Licensed User
Longtime User
DoEvents will not help here.
You should send the file length before sending the file itself. Then in your server you should read until you read the exact length.

Can you elaborate?

Using HttpUtils and the AdvancedCamera library I can POST images to a server as a byte array (specifically, using InitializePost2)

But in PHP I have absolutely no idea how I'd create a "listener" to read in the byte array until every element has been read in.
 

mcmanu

Active Member
Licensed User
Longtime User
Rotate the Panel

Hi,
I have to rotate the panel that i can use the camera in the Horizontal and not in the vertikal format. How can i do this?
 
Status
Not open for further replies.
Top