Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Take pictures with the internal camera

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-14-2010, 02:02 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Take pictures with the internal camera

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.



The following code saves the image:
Code:
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.
Attached Files
File Type: zip CameraExample.zip (5.7 KB, 1068 views)
Reply With Quote
  #2 (permalink)  
Old 12-14-2010, 02:19 PM
Cor Cor is offline
Basic4ppc Veteran
 
Join Date: May 2007
Posts: 481
Default

great

thanks

grCor
Reply With Quote
  #3 (permalink)  
Old 02-16-2011, 03:48 AM
Junior Member
 
Join Date: Feb 2011
Posts: 21
Default I get an error

when I run this example, I get an error after I click on the button to take a picture. Here is what it says:

An error has occurred in sub:main_camera1_picturetaken
(java line:225)
java.io.FileNotFoundException: /sdcard/1.jpg
Continue?

also, after looking at the code, I noticed there is a SUB that NEVER gets called, the sub is :
Code:
Sub Camera1_Ready (Success As Boolean)
    
If success Then
        Camera1.StartPreview
        btnTakePicture.Enabled = 
True
    
Else
        
ToastMessageShow("Cannot open camera."True)
    
End If
End Sub
not sure what if I am doing something wrong.

edit:

unless after you create Camera1, this sub becomes an event. This is so confusing.

Last edited by mshihrer : 02-16-2011 at 03:52 AM.
Reply With Quote
  #4 (permalink)  
Old 02-16-2011, 06:22 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The Ready event is clearly described in the first post.

Do you have an external card mounted? Make sure that it is not locked for some reason (like when the device is connected as a USB storage).
Reply With Quote
  #5 (permalink)  
Old 02-17-2011, 12:17 AM
Junior Member
 
Join Date: Feb 2011
Posts: 21
Default

ok, thanks, that was it. I still had it connected to usb.
Sorry about the event question, I get it now. I never noticed the section in the documentation for the libraries that list the events. All good now, thanks again.
Reply With Quote
  #6 (permalink)  
Old 02-18-2011, 03:19 PM
Junior Member
 
Join Date: Jan 2011
Posts: 44
Default

How would we use this to take VIDEO? Also, my tablet has a front facing camera only, will this still work?

Mike
Reply With Quote
  #7 (permalink)  
Old 02-20-2011, 07:11 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
How would we use this to take VIDEO?
Only still images are supported for now.
Quote:
Also, my tablet has a front facing camera only, will this still work?
Assuming that Android treats it as the primary camera then it should work properly.
Reply With Quote
  #8 (permalink)  
Old 03-09-2011, 12:37 PM
Knows the basics
 
Join Date: Jan 2011
Posts: 73
Question problem orientation

Hi, I found that if the orientation of the application is Both the preview does not display if the orientation is Portrait everything goes right. How can we solve this? Thanks


my English = google translator (sorry)
Reply With Quote
  #9 (permalink)  
Old 03-09-2011, 12:45 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Currently only landscape is supported (this is the native camera orientation).
Reply With Quote
  #10 (permalink)  
Old 03-09-2011, 01:08 PM
Knows the basics
 
Join Date: Jan 2011
Posts: 73
Default

And is there any way to change the view of an activity without fixing the view of the entire application?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Internal help Erel Basic4android Updates and Questions 6 10-15-2010 03:59 PM
TakePicture - Using the internal camera from your application Erel Code Samples & Tips 16 11-21-2009 10:52 AM
Displaying pictures from the internet N1c0_ds Questions (Windows Mobile) 7 12-04-2008 10:49 PM
external or internal GPS? Georg Questions (Windows Mobile) 4 07-31-2008 04:56 PM
import pictures into prgram 2 stay cosmo15900 Questions (Windows Mobile) 4 01-15-2008 05:58 PM


All times are GMT. The time now is 10:23 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0