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

rfresh

Well-Known Member
Licensed User
Is there any way to turn off the camera clicking sound when it takes the picture? I'm setting up a spare phone at home to take pictures of a weather screen. I'd like it to just sit there and take pictures quietly.
 

vb1992

Well-Known Member
Licensed User
phone library has volume settings


B4X:
SetVolume (Channel As Int, VolumeIndex As Int, ShowUI As Boolean)

Sets the volume of the specified channel.

Channel - One of the VOLUME constants.

VolumeIndex - The volume index. GetMaxVolume can be used to find the largest possible value.

ShowUI - Whether to show the volume UI windows.


Example:
Dim p As Phone
p.SetVolume(p.VOLUME_MUSIC, 3, True)
  
 VOLUME_ALARM As Int

Alarms channel.
 VOLUME_MUSIC As Int

Music channel.
 VOLUME_NOTIFICATION As Int

Notifications channel.
 VOLUME_RING As Int

Phone ring channel.
 VOLUME_SYSTEM As Int

System sounds channel.
 VOLUME_VOICE_CALL As Int

Voice calls channel.
 

rfresh

Well-Known Member
Licensed User
Thank you. I've implemented phone volume code in my app previously and have tried turning down the volume on all 5 channels but the camera click sound is still there. It's either controlled by another sound channel or we can't control it?
 

askncan

New Member
Hello Erel

Hello Erel, :sign0087:
And i am sorry about this silly questions but the app only can take 1 picture, have i missed something :D
 

mrjaw

Active Member
Licensed User
Hi!
This is true ? Just take one photo ? I am doing an app that takes a few photos and send to my server by FTP. It can be posible using the camera and fTP lib?
How can I take a few photos ?
 

birnesoft

Active Member
Licensed User
CameraSound still makes me crazy

I want to take photos while audio recording.
Does anybody solved the problem with the cameraSound.
I've also tried what Erel said.

IDim p As Phone
p.SetMute(p.VOLUME_ALARM,True)
p.SetMute(p.VOLUME_MUSIC,True)
p.SetMute(p.VOLUME_NOTIFICATION,True)
p.SetMute(p.VOLUME_RING,True)
p.SetMute(p.VOLUME_SYSTEM,True)
p.SetMute(p.VOLUME_VOICE_CALL,True)

but the SOUND of the camera is still here. Maybe it's on an other channel.

any ideas?

Gruss Björn
 

birnesoft

Active Member
Licensed User
Seems like it is not possible to do it with Android??

Some Apps do it. Camera without Click Sound.

or you mean:
Seems like it is not possible to do it with B4A

If there is a way in Java, it's possiple to implement this by writing a lib?
or we have to wait for next Version of ACL

thanks Björn
 
Last edited:

birnesoft

Active Member
Licensed User
Turn off camera shutter sound

I didn't find a way to implement it with Java. If you find then it will be possible to port it to Basic4android.

here I've found an example. How I can implement this in B4A?

turn-off-camera-shutter-sound

package com.example.picture;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.AudioManager;
import android.os.IBinder;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraService extends Service
{
AudioManager mgr ;
//Camera variables
//a surface holder
private SurfaceHolder sHolder;
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
/** Called when the activity is first created. */
@Override
public void onCreate()
{
super.onCreate();

Log.d("Entered","dfdf");

}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);

mCamera = Camera.open();
SurfaceView sv = new SurfaceView(getApplicationContext());
mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);


try {
mCamera.setPreviewDisplay(sv.getHolder());
parameters = mCamera.getParameters();

//set camera parameters
mCamera.setParameters(parameters);
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//Get a surface
sHolder = sv.getHolder();


//tells Android that this surface will have its data constantly replaced
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}



Camera.PictureCallback mCall = new Camera.PictureCallback()
{

public void onPictureTaken(byte[] data, Camera camera)
{
//decode the data obtained by the camera into a Bitmap

FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/Image.jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
} catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);
}
};


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
 
Last edited:

birnesoft

Active Member
Licensed User
Yes it looks like.
Thanks for your fast answers.
You give the best support I know.

Congratulations and thanks for your

20000 Posts.:wav:
 

bjf

Member
Licensed User
Yes.
B4X:
Dim in As InputStream
in.InitializeFromBytesArray(data, 0, data.Length)
Dim bmp As Bitmap
bmp.Initialize2(in)

I'm sorry for bring in up this old thread but this is exactly what i need to do as well.
Can however not get the picture to display using this, is there something else that needs to be done?
I have put the above code right after the photo is taken.
 

birnesoft

Active Member
Licensed User
I'm sorry for bring in up this old thread but this is exactly what i need to do as well.
Can however not get the picture to display using this, is there something else that needs to be done?
I have put the above code right after the photo is taken.

to Display the picture you need an imageView or a Panel

dim p as Panel
p.initialize("")
p.setbackgroundimage(in)
 

Beja

Expert
Licensed User
Hi Erel,

The picture data is stored in bytes. What does 1 byte represent?
my guess is it represents attributes of 1 pixel (color, intensity...etc).
another question: how the picture is sampled? (left-to-right and then down)?

Asking these questions to see if I can try to process the picture digitally.

Thanks in advance.
 
Status
Not open for further replies.
Top