2222 */
2323package com .nitobi .phonegap .api .impl ;
2424
25+ import java .io .ByteArrayOutputStream ;
26+ import java .io .IOException ;
27+ import java .io .InputStream ;
28+
29+ import javax .microedition .io .Connector ;
30+ import javax .microedition .io .file .FileConnection ;
31+
2532import net .rim .blackberry .api .invoke .CameraArguments ;
2633import net .rim .blackberry .api .invoke .Invoke ;
34+ import net .rim .device .api .io .Base64OutputStream ;
2735import net .rim .device .api .io .file .FileSystemJournal ;
2836import net .rim .device .api .io .file .FileSystemJournalEntry ;
2937import net .rim .device .api .io .file .FileSystemJournalListener ;
3240import net .rim .device .api .system .EventInjector ;
3341import net .rim .device .api .ui .UiApplication ;
3442
43+ import com .nitobi .phonegap .PhoneGap ;
3544import com .nitobi .phonegap .api .Command ;
3645
3746/**
4251 */
4352public class CameraCommand implements Command {
4453
45- private static final int INVOKE_COMMAND = 0 ;
46- private static final int PICTURE_COMMAND = 1 ;
54+ private static final int PICTURE_COMMAND = 0 ;
4755 private static final String CODE = "PhoneGap=camera" ;
48- // private static final String
56+ private static final String CAMERA_ERROR_CALLBACK = ";if (navigator.camera.onError) { navigator.camera.onError(); }" ;
4957
5058 private long lastUSN = 0 ;
5159 private String photoPath ;
60+ private String returnVal ;
5261 private FileSystemJournalListener listener ;
62+ private PhoneGap berryGap ;
5363
54- public CameraCommand () {
64+ public CameraCommand (PhoneGap phoneGap ) {
65+ berryGap = phoneGap ;
5566 listener = new FileSystemJournalListener () {
5667 public void fileJournalChanged () {
5768 long USN = FileSystemJournal .getNextUSN ();
5869 for (long i = USN - 1 ; i >= lastUSN ; --i ) {
5970 FileSystemJournalEntry entry = FileSystemJournal .getEntry (i );
6071 if (entry != null ) {
61- if (entry .getEvent () == FileSystemJournalEntry .FILE_ADDED || entry . getEvent () == FileSystemJournalEntry . FILE_CHANGED || entry . getEvent () == FileSystemJournalEntry . FILE_RENAMED ) {
72+ if (entry .getEvent () == FileSystemJournalEntry .FILE_CHANGED ) {
6273 if (entry .getPath ().indexOf (".jpg" ) != -1 ) {
6374 lastUSN = USN ;
6475 photoPath = entry .getPath ();
76+
77+ InputStream theImage ;
78+ byte [] imageBytes ;
79+ Base64OutputStream base64OutputStream = null ;
80+ try {
81+ FileConnection fconn = (FileConnection )Connector .open ("file://" + photoPath );
82+ imageBytes = new byte [(int ) fconn .fileSize ()];
83+ theImage = fconn .openInputStream ();
84+ theImage .read (imageBytes );
85+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ( imageBytes .length );
86+ base64OutputStream = new Base64OutputStream ( byteArrayOutputStream );
87+ base64OutputStream .write (imageBytes );
88+ base64OutputStream .flush ();
89+ base64OutputStream .close ();
90+ byteArrayOutputStream .flush ();
91+ byteArrayOutputStream .close ();
92+ //int sizeofbase64 = byteArrayOutputStream.toString().length();
93+ returnVal = ";if (navigator.camera.onSuccess) { navigator.camera.onSuccess('" +byteArrayOutputStream .toString ()+"'); }" ;
94+ } catch (IOException e ) {
95+ e .printStackTrace ();
96+ returnVal = CAMERA_ERROR_CALLBACK ;
97+ }
98+ berryGap .pendingResponses .addElement (returnVal );
6599 closeCamera ();
66100 }
67101 }
@@ -81,25 +115,24 @@ public boolean accept(String instruction) {
81115 public String execute (String instruction ) {
82116 switch (getCommand (instruction )) {
83117 case PICTURE_COMMAND :
84- UiApplication .getUiApplication ().removeFileSystemJournalListener (listener );
85- return ";navigator.camera.picture = '" + photoPath + "';" ;
86- case INVOKE_COMMAND :
87118 photoPath = null ;
119+ returnVal = null ;
88120 UiApplication .getUiApplication ().addFileSystemJournalListener (listener );
89121 Invoke .invokeApplication (Invoke .APP_TYPE_CAMERA , new CameraArguments ());
122+ return "" ;
90123 }
91124 return null ;
92125 }
93126
94127 private int getCommand (String instruction ) {
95128 String command = instruction .substring (instruction .lastIndexOf ('/' ) + 1 );
96- if ("obtain" .equals (command )) return INVOKE_COMMAND ;
97129 if ("picture" .equals (command )) return PICTURE_COMMAND ;
98130 return -1 ;
99131 }
100132
101133 public void closeCamera () {
102134 try {
135+ UiApplication .getUiApplication ().removeFileSystemJournalListener (listener );
103136 EventInjector .KeyEvent inject = new EventInjector .KeyEvent (EventInjector .KeyEvent .KEY_DOWN , Characters .ESCAPE , 0 );
104137 inject .post ();
105138 inject .post ();
0 commit comments