Take a Picture without a GUI

All your problems with Audio, Video and Images.

Re: Take a Picture without a GUI

Postby canadiancow » Wed Aug 04, 2010 2:39 pm

blundell wrote:Hey, I haven't got the answer atm but I'll just tease you a little and say it can be done :-) if you check out some of the current apps on the market. Maybe email the dev's they may be kind enough to share the code snippet, an if so share with us ;-)


Can you give us the names of some of these applications?
canadiancow
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 31, 2009 2:36 pm

Top

Re: Take a Picture without a GUI

Postby blundell » Wed Aug 04, 2010 2:49 pm

'SpyShot'
'Spy Camera Lite'

I think you get the jist of the search terms
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Take a Picture without a GUI

Postby canadiancow » Wed Aug 04, 2010 3:00 pm

blundell wrote:'SpyShot'
'Spy Camera Lite'

I think you get the jist of the search terms


The description for both of those apps implies there's still a GUI.

After downloading them both, I can see they both need a GUI. SpyShot gives you the option to "black out" the screen, whereas Spy Camera Lite does it by default. So neither of them display the preview, but they both need to be running in the foreground.

I'm not sure of SirBastian's requirements, but I need to have all this done from a Service (i.e. no Activity), because I want to be able to run other applications as I'm taking pictures.
canadiancow
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 31, 2009 2:36 pm

Re: Take a Picture without a GUI

Postby blundell » Wed Aug 04, 2010 3:09 pm

I was just thinking, how do they black out the GUI? And could you use this type of implementation in a service. It all depends on how they 'black it out'
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Take a Picture without a GUI

Postby SirBastian » Wed Aug 04, 2010 5:59 pm

I managed to start a Service that used ScheduledExecutor to periodically run the takePicture method, and then store the jpegs on the sdcard in the jpegCallback. Curiously, this service continues to function even if the activity is minimized. A clue, perhaps?
SirBastian
Freshman
Freshman
 
Posts: 4
Joined: Sat Jul 31, 2010 7:18 am

Re: Take a Picture without a GUI

Postby canadiancow » Wed Aug 04, 2010 6:11 pm

SirBastian wrote:I managed to start a Service that used ScheduledExecutor to periodically run the takePicture method, and then store the jpegs on the sdcard in the jpegCallback. Curiously, this service continues to function even if the activity is minimized. A clue, perhaps?


That seems somewhat more useful than the two apps in the Market, and I'll give it a try, but it's still a hack around a poor implementation.
canadiancow
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 31, 2009 2:36 pm

Top

Re: Take a Picture without a GUI

Postby SirBastian » Wed Aug 04, 2010 6:18 pm

Yeah, it doesn't avoid the issue of needing the preview stream when the UI is open. I find it bizarre that the preview can be paused, but not hidden using setVisibility. Actually, I find it odd that it needs to be drawn at all. Why on Earth would some of the setup be hidden inside startPreview?
SirBastian
Freshman
Freshman
 
Posts: 4
Joined: Sat Jul 31, 2010 7:18 am

Re: Take a Picture without a GUI

Postby canadiancow » Wed Aug 04, 2010 7:03 pm

SirBastian wrote:Yeah, it doesn't avoid the issue of needing the preview stream when the UI is open. I find it bizarre that the preview can be paused, but not hidden using setVisibility. Actually, I find it odd that it needs to be drawn at all. Why on Earth would some of the setup be hidden inside startPreview?


I actually went through a bit of the Android source when I was first looking into this issue. Unfortunately, there were so many calls back and forth between C and Java (since this is dealing with hardware), that I gave up.
canadiancow
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 31, 2009 2:36 pm

Re: Take a Picture without a GUI

Postby SirBastian » Tue Aug 17, 2010 4:48 am

Got it working. Here's the code for a Service class called CamService that automatically takes a picture every 5 seconds as soon as it starts.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CamService extends Service {
  2.        
  3.         private static final String TAG = "CameraDemo";
  4.        
  5.         private final ScheduledExecutorService scheduler =
  6.                Executors.newScheduledThreadPool(1);
  7.        
  8.         Camera camera;
  9.         ScheduledFuture<?> beeperHandle;
  10.        
  11.        
  12.         /* Service Life cycle Overrides */
  13.         @Override
  14.         public void onCreate() {
  15.                 super.onCreate();
  16.                 camera = Camera.open();
  17.                 try {
  18.                         camera.setPreviewDisplay(null);
  19.                 } catch (IOException e) {
  20.                         e.printStackTrace();
  21.                 }
  22.                 takePicsPeriodically(5);
  23.         }
  24.        
  25.         @Override
  26.         public void onDestroy() {
  27.                 stopPics();
  28.                 super.onDestroy();
  29.         }
  30.  
  31.         @Override
  32.         public IBinder onBind(Intent arg0) {
  33.                 return myRemoteServiceStub;
  34.         }
  35.         /* ----------------------------------- */
  36.        
  37.         private IMyRemoteService.Stub myRemoteServiceStub = new IMyRemoteService.Stub() {
  38.                
  39.                 /* Basic Service Methods */
  40.                 public boolean isCollecting() {
  41.                         return (beeperHandle != null);
  42.                 }
  43.                 /* ------------------------- */
  44.         };
  45.        
  46.         public void takePicsPeriodically(long period) {
  47.         final Runnable beeper = new Runnable() {
  48.                 public void run() { camera.takePicture(shutterCallback,rawCallback,jpegCallback); }
  49.             };
  50.         camera.startPreview();
  51.         beeperHandle = scheduler.scheduleAtFixedRate(beeper, period, period, TimeUnit.SECONDS);
  52.     }
  53.        
  54.         public void stopPics() {
  55.                 beeperHandle.cancel(true);
  56.                 beeperHandle = null;
  57.                 camera.stopPreview();
  58.         camera.release();
  59.         camera = null;
  60.         }
  61.        
  62.         /* Camera Call backs */
  63.         ShutterCallback shutterCallback = new ShutterCallback() {
  64.                 public void onShutter() {
  65.                         Log.d(TAG, "onShutter'd");
  66.                 }
  67.         };
  68.  
  69.         /** Handles data for raw picture */
  70.         PictureCallback rawCallback = new PictureCallback() {
  71.                 public void onPictureTaken(byte[] data, Camera camera) {
  72.                         Log.d(TAG, "onPictureTaken - raw");
  73.                 }
  74.         };
  75.  
  76.         /** Handles data for j peg picture */
  77.         PictureCallback jpegCallback = new PictureCallback() {
  78.                 public void onPictureTaken(byte[] data, Camera camera) {
  79.                         FileOutputStream outStream = null;
  80.                         try {
  81.                                 // write to local sand box file system
  82.                                 //outStream = CameraDemo.this.openFileOutput(String.format("%d.jpg", System.currentTimeMillis()), 0);  
  83.                                 // Or write to s d card
  84.                                 outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
  85.                                 outStream.write(data);
  86.                                 outStream.close();
  87.                                 Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
  88.                         } catch (FileNotFoundException e) {
  89.                                 e.printStackTrace();
  90.                         } catch (IOException e) {
  91.                                 e.printStackTrace();
  92.                         } finally {
  93.                         }
  94.                         Log.d(TAG, "onPictureTaken - jpeg");
  95.                 }
  96.         };
  97.  
  98.  
  99. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
SirBastian
Freshman
Freshman
 
Posts: 4
Joined: Sat Jul 31, 2010 7:18 am

Re: Take a Picture without a GUI

Postby blundell » Tue Aug 17, 2010 8:17 am

Very nice GJ, guess it'd be easy to convert it to video as well
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Take a Picture without a GUI

Postby mob_king » Thu Aug 26, 2010 10:00 am

SirBastian wrote:Got it working. Here's the code for a Service class called CamService that automatically takes a picture every 5 seconds as soon as it starts.


So can we modify CamService to record video as well ? I am too looking for a solution for recording video from a Service without any GUI. Is it possible ?
mob_king
Developer
Developer
 
Posts: 35
Joined: Mon Apr 12, 2010 5:19 pm

Re: Take a Picture without a GUI

Postby canadiancow » Thu Aug 26, 2010 1:03 pm

mob_king wrote:
SirBastian wrote:Got it working. Here's the code for a Service class called CamService that automatically takes a picture every 5 seconds as soon as it starts.


So can we modify CamService to record video as well ? I am too looking for a solution for recording video from a Service without any GUI. Is it possible ?


I haven't been able to get it to work. If you call MediaRecorder.prepare() without having set a non-null preview surface, it crashes.
canadiancow
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Aug 31, 2009 2:36 pm

Re: Take a Picture without a GUI

Postby ready1 » Wed Oct 09, 2013 1:39 am

I found this app that does exactly what you need and more, its called AutoPIX....

https://play.google.com/store/apps/deta ... be.autopix
ready1
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 09, 2013 1:36 am

Top
Previous

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 4 guests