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,117

mcmanu

Active Member
Licensed User
Longtime User
danke

Danke hab jetzt camera1.OriPortrait benutzt, er zeigt mir eine fehlermeldung an wenn ich die kamera starte --> java.lang.RuntimeEcexption: set display orientation failed. Komischerweise wenn ich des Programm weiterausführen lasse wurde aber die Orientation richtig gesetzt und es funktioniert auch. Warum kommt diese Fehlermeldung trozdem?

Danke :)
 

mcmanu

Active Member
Licensed User
Longtime User
hi

upps sorry i Write it down in german :/ soorry ;)
Okay i will post my code later :)
Thank you for your help :)
 

mcmanu

Active Member
Licensed User
Longtime User
My code

Hi heres the code,

Its the same like in the example.

Sub Camera1_PictureTaken (Data() As Byte)

camera1.StartPreview
camera1.OriPortrait

Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
out.WriteBytes(data, 0, data.Length)
out.Close
ToastMessageShow("Dein Bild wurde gespeichert: " & File.Combine(File.DirRootExternal, "1.jpg"), True)

button5.Enabled = True
End Sub

Sub Button5_Click
button5.Enabled = False
camera1.TakePicture
label4.Text = "Dein bild wurde gespeichert"
end sub

in an other sub i wrote this -->

Camera1_Ready(success)
Dim data() As Byte
Camera1_PictureTaken(data)
 

mcmanu

Active Member
Licensed User
Longtime User
Oriportrait

Nope it doesn´t work

But i fixed it, and now it works :) i used try catch

code -->
Sub Camera1_Ready (Success As Boolean)

If success Then
Try
camera1.OriPortrait
Catch
End Try
Camera1.StartPreview
button5.Enabled = True
Else
ToastMessageShow("Kamera konnte nicht geöffnet werden.", True)
End If
End Sub
 

askez

Member
Licensed User
Longtime User
camera sound

how can i use this application without the taking picture sound?
 

ramil

New Member
Licensed User
Longtime User
Using the Camera as a sensor

Hello,

My android device is turned on all the time and I want to save power and to save the screen.
Is it possible to use the camera as a simple movement sensor ?
I want my app to recognize a movement (by compering the current picture to the last picture?) high resolution is not needed...

Thanks!
 

Mickster

Active Member
Licensed User
Longtime User
Hey,
is there a way to display the image taken without having to load it from storage? My user needs the option to discard the image and take another.
 

sterlingy

Active Member
Licensed User
Longtime User
You can manually edit the manifest file and set the orientation property of a specific activity. Then set the file to be read-only so it would not be overwritten.
The file - AndroidManifest.Xml is located in <source folder>\Objects.
B4X:
 <activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name=".main"
                  android:label="Flickr Viewer" android:screenOrientation="unspecified">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="activity2" android:label="Activity2" android:screenOrientation="unspecified"></activity>
You should change "unspecified" to "landscape" for the required activity.

It is recommended to do this change when your application is more or less ready. Once you set the file to read-only it will not get updated with new permissions / updated version number and so on.

Erel,

I modified my Manifest and locked the file, but When I switch to the Camera Activity, the phone does not switch into "landscape."

Have I edited the Manifest incorrectly?

B4X:
<?xml version="1.0" encoding="utf-8"?>
<manifest
   xmlns:android="http://schemas.android.com/apk/res/android"
   package="B4A.UserInterfaceButtonToolbox"
   android:versionCode="1"
   android:versionName=""
   android:installLocation="internalOnly">
   
   <uses-sdk android:minSdkVersion="4" />
   <supports-screens android:largeScreens="true" 
       android:normalScreens="true" 
       android:smallScreens="true" 
       android:anyDensity="true"/>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.CAMERA"/>
   <uses-permission android:name="android.permission.INTERNET"/>
   <application
      android:icon="@drawable/icon"
      android:label="My School Online">
      <activity
         android:windowSoftInputMode="stateHidden"
         android:launchMode="singleTop"
         android:name=".main"
         android:label="My School Online"
         android:screenOrientation="unspecified">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      <activity 
         android:windowSoftInputMode="stateHidden"                android:launchMode="singleTop"                      android:name="Camera" 
         android:label="Camera"                         android:screenOrientation="landscape">
      </activity>
   </application>
</manifest>

Regards,

Sterling
 

sterlingy

Active Member
Licensed User
Longtime User
Erel,

Thanks for getting back to me.

I tried this and I got an error, when trying to compile: "Module: camera not found."

I haven't worked with multiple Activities before, though I did change the Title of the Camera layout to "Camera," but it still shows up in the Designer dropdown list as Activity, so I am assuming this is not where to set this parameter.

Cheers,

Sterling
 

sterlingy

Active Member
Licensed User
Longtime User
Erel,

I went through the Activity documentation and the Tutorial on the forum. I got everything working.

Just one thing: When I return to the "main" Activity, my app goes back to the starting screen, which is a login screen. Am I right in that I need to save which Layout I am on in a variable, before starting the "camera" activity, and on RESUME, read the variable and jump to that Layout?

If so, is it best to do this in a Process Variable?

I should point out that this happens when clicking a Button I made to return to the previous Activity. Using the phone's BACK button goes to the previous Layout.

-Sterling
 
Last edited:
Status
Not open for further replies.
Top