Changeset 1460

Show
Ignore:
Timestamp:
11/10/08 04:26:07 (2 months ago)
Author:
czue
Message:

Updating the flow of the image choosing activity. Wired file browsing and image capture with it, which each return thumbnails that are displayed in the chooser. updated the destroy code and flow for the image capture to manage resources.

Location:
branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image
Files:
5 modified
2 moved

Legend:

Unmodified
Added
Removed
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/activity/FileBrowseActivity.java

    r1455 r1460  
    44import java.io.InputStream; 
    55import java.util.Enumeration; 
     6import java.util.Hashtable; 
    67 
    78import javax.microedition.io.Connector; 
     
    2324import org.javarosa.core.api.IShell; 
    2425import org.javarosa.j2me.view.DisplayViewFactory; 
     26import org.javarosa.media.image.model.FileDataPointer; 
    2527import org.javarosa.media.image.utilities.FileUtility; 
    2628 
     
    4143        private final static String SEP_STR = "/"; 
    4244        private final static char SEP = '/'; 
     45 
     46        public static final String FILE_POINTER = "FILE_POINTER"; 
    4347 
    4448        public FileBrowseActivity(IShell shell) { 
     
    103107                        List curr = (List) d; 
    104108                        final String currFile = curr.getString(curr.getSelectedIndex()); 
    105                          
    106109                        new Thread(new Runnable() { 
    107110                                public void run() { 
     
    184187 
    185188        void returnFile(String fileName) { 
    186                 try { 
    187                         // todo 
    188                 } catch (Exception e) { 
    189                 } 
     189                String fullName = "file:///" + currDirName + fileName; 
     190                FileDataPointer fdp = new FileDataPointer(fullName); 
     191                Hashtable returnArgs = new Hashtable(); 
     192                returnArgs.put(FILE_POINTER, fdp); 
     193                shell.returnFromActivity(this, Constants.ACTIVITY_COMPLETE, returnArgs); 
    190194        } 
    191195         
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/activity/ImageCaptureActivity.java

    r1455 r1460  
    22 
    33import java.io.IOException; 
    4 import java.io.OutputStream; 
    5 import java.util.Enumeration; 
    64import java.util.Hashtable; 
    75 
    8 import javax.microedition.io.Connector; 
    9 import javax.microedition.io.file.FileConnection; 
    10 import javax.microedition.io.file.FileSystemRegistry; 
    116import javax.microedition.lcdui.Alert; 
    127import javax.microedition.lcdui.AlertType; 
     
    149import javax.microedition.lcdui.Command; 
    1510import javax.microedition.lcdui.CommandListener; 
    16 import javax.microedition.lcdui.Display; 
    1711import javax.microedition.lcdui.Displayable; 
    1812import javax.microedition.lcdui.Image; 
     
    2822import org.javarosa.core.api.IDisplay; 
    2923import org.javarosa.core.api.IShell; 
     24import org.javarosa.core.model.data.BasicDataPointer; 
    3025import org.javarosa.j2me.view.DisplayViewFactory; 
    3126import org.javarosa.media.image.model.FileDataPointer; 
    32 import org.javarosa.media.image.storage.ImageRMSUtility; 
     27import org.javarosa.media.image.storage.FileRMSUtility; 
    3328import org.javarosa.media.image.utilities.FileUtility; 
    3429import org.javarosa.media.image.view.CameraCanvas; 
     
    5651        private Command mCaptureCommand; 
    5752        private IDisplay display; 
    58         private ImageRMSUtility dataModel; 
    59         private Image image; 
     53        private FileRMSUtility dataModel; 
     54        private byte[] imageData; 
     55        private int width; 
     56        private int height; 
    6057         
    6158        public ImageCaptureActivity(IShell shell) { 
    6259                this.shell = shell; 
    6360                display = JavaRosaServiceProvider.instance().getDisplay(); 
    64                 dataModel = new ImageRMSUtility("image_store"); 
    65         } 
    66  
     61                dataModel = new FileRMSUtility("image_store"); 
     62                width = 640; 
     63                height = 480; 
     64        } 
     65 
     66         
    6767        public void contextChanged(Context globalContext) { 
    6868                // TODO Auto-generated method stub 
     
    101101        } 
    102102         
     103        public void setResolition(int width, int height) { 
     104                this.width = width; 
     105                this.height = height; 
     106                 
     107        } 
    103108        /** 
    104109         * Actually capture an image 
     
    126131                // stick the picture in here.  
    127132                Hashtable table = new Hashtable(); 
    128                 table.put(IMAGE_KEY, image); 
     133                BasicDataPointer p = new BasicDataPointer("Image", imageData); 
     134                table.put(IMAGE_KEY, p); 
    129135                return table; 
    130136        } 
     137         
    131138        private void showCamera() { 
    132139                try { 
     
    186193 
    187194        private void doCapture() { 
    188                 byte[] jpg;      
    189                 int width = 640; 
    190                 int height = 480; 
    191195                try { 
    192196                        // Get the image. 
    193                         jpg = mVideoControl.getSnapshot("encoding=jpeg&quality=100&width=" + width + "&height=" + height); 
    194                         image = Image.createImage(jpg, 0, jpg.length); 
     197                        imageData = mVideoControl.getSnapshot("encoding=jpeg&quality=100&width=" + width + "&height=" + height); 
     198                        //image = Image.createImage(jpg, 0, jpg.length); 
    195199                        doFinish(); 
    196200                        // Save to file no longer 
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/activity/ImageChooserActivity.java

    r1455 r1460  
    1616import org.javarosa.core.api.IDisplay; 
    1717import org.javarosa.core.api.IShell; 
     18import org.javarosa.core.model.data.IDataPointer; 
    1819import org.javarosa.j2me.view.DisplayViewFactory; 
    1920import org.javarosa.media.image.model.FileDataPointer; 
    20 import org.javarosa.media.image.storage.ImageRMSUtility; 
     21import org.javarosa.media.image.storage.FileRMSUtility; 
    2122import org.javarosa.media.image.utilities.ImageUtility; 
    2223 
    2324/** 
    24  * An Activity that represents the selection of zero or more images. 
    25  * This will launch a UI that supports displaying a list of images, marking 
    26  * some subset of them and returning those images.  New images can also  
    27  * be added via an ImageCaptureActivity. 
     25 * An Activity that represents the selection of zero or more images. This will 
     26 * launch a UI that supports displaying a list of images, marking some subset of 
     27 * them and returning those images. New images can also be added via an 
     28 * ImageCaptureActivity. 
     29 *  
    2830 * @author Cory Zue 
    29  * 
     31 *  
    3032 */ 
    31 public class ImageChooserActivity implements IActivity, CommandListener 
    32 { 
     33public class ImageChooserActivity implements IActivity, CommandListener { 
    3334        public static final String ACTIVITY_KEY = "ACTIVITY_KEY"; 
    34         // this should map images (IDataPointers) to bool true/false whether selected or not 
     35        // this should map images (IDataPointers) to bool true/false whether 
     36        // selected or not 
    3537        private Hashtable allImages; 
    3638        private Context context; 
    3739        private IShell shell; 
    3840        private IDisplay display; 
    39         private ImageRMSUtility dataModel; 
     41        private FileRMSUtility dataModel; 
    4042        private Form mainForm; 
    4143        private Command cancelCommand; 
     
    4648        private Command markCommand; 
    4749        private String currentKey; 
    48            
    4950 
    5051        public ImageChooserActivity(IShell shell) { 
    5152                this.shell = shell; 
    5253                display = JavaRosaServiceProvider.instance().getDisplay(); 
    53                 dataModel = new ImageRMSUtility("image_store"); 
    54                  
     54                dataModel = new FileRMSUtility("image_store"); 
     55 
    5556                cancelCommand = new Command("Cancel", Command.CANCEL, 0); 
    5657                returnCommand = new Command("Return", Command.OK, 0); 
    57             cameraCommand = new Command("Camera", Command.SCREEN, 0); 
    58             browseCommand = new Command("Browse", Command.SCREEN, 0); 
    59             viewCommand = new Command("View", Command.SCREEN, 0); 
    60             markCommand = new Command("Mark/Unmark", Command.SCREEN, 0); 
     58                cameraCommand = new Command("Camera", Command.SCREEN, 0); 
     59                browseCommand = new Command("Browse", Command.SCREEN, 0); 
     60                viewCommand = new Command("View", Command.SCREEN, 0); 
     61                markCommand = new Command("Mark/Unmark", Command.SCREEN, 0); 
    6162        } 
    6263 
    6364        public void contextChanged(Context globalContext) { 
    6465 
    65                  
    6666        } 
    6767 
    6868        public void destroy() { 
    6969                // TODO Auto-generated method stub 
    70                  
     70 
    7171        } 
    7272 
     
    7777        public void halt() { 
    7878                // TODO Auto-generated method stub 
    79                  
     79 
    8080        } 
    8181 
    8282        public void resume(Context globalContext) { 
    83                 Image img = (Image) globalContext.getElement(currentKey); 
    84                 if (img != null) { 
    85                         Image thumbnail = ImageUtility.createThumbnail(img); 
    86                         mainForm.append(thumbnail); 
     83                Object o = globalContext.getElement(currentKey); 
     84                IDataPointer pointer = (IDataPointer) o; 
     85                try { 
     86                        if (pointer != null) {  
     87                                byte[] data = pointer.getData(); 
     88                                Image img = Image.createImage(data, 0, data.length); 
     89                                Image thumbNail = ImageUtility.createThumbnail(img); 
     90                                mainForm.append(thumbNail); 
     91                        } 
     92                } catch (Exception ex) { 
     93                        System.out.println(ex.getMessage()); 
     94                        System.out.println(ex); 
     95                         
    8796                } 
    8897                display.setView(DisplayViewFactory.createView(mainForm)); 
    89                  
    9098        } 
    9199 
    92100        public void setShell(IShell shell) { 
    93101                this.shell = shell; 
    94                  
     102 
    95103        } 
    96104 
     
    98106                this.context = context; 
    99107                mainForm = new Form("Image Chooser"); 
    100         mainForm.addCommand(cancelCommand); 
     108                mainForm.addCommand(cancelCommand); 
    101109                mainForm.addCommand(cameraCommand); 
    102110                mainForm.addCommand(browseCommand); 
     
    104112                mainForm.addCommand(viewCommand); 
    105113                mainForm.addCommand(markCommand); 
    106             mainForm.setCommandListener(this); 
    107              
    108             display.setView(DisplayViewFactory.createView(mainForm)); 
    109                  
    110         } 
    111          
     114                mainForm.setCommandListener(this); 
     115 
     116                display.setView(DisplayViewFactory.createView(mainForm)); 
     117 
     118        } 
     119 
    112120        private FileDataPointer captureNewImage() { 
    113                 // TODO: I have a feeling this is going to actually be some sort of back and forth chicannery 
     121                // TODO: I have a feeling this is going to actually be some sort of back 
     122                // and forth chicannery 
    114123                // with the shell 
    115124                return null; 
    116125        } 
    117          
     126 
    118127        /** 
    119          * Selects an image  
     128         * Selects an image 
     129         *  
    120130         * @param image 
    121131         */ 
     
    123133                // TODO: impl; 
    124134        } 
    125          
     135 
    126136        /** 
    127          * Deselects an image  
     137         * Deselects an image 
     138         *  
    128139         * @param image 
    129140         */ 
     
    131142                // TODO: impl; 
    132143        } 
    133          
     144 
    134145        /** 
    135146         * Picks the selected images and returns them (and control) to the shell 
     
    137148         */ 
    138149        private void finish() { 
    139                 Hashtable args = null; //buildReturnArgs(); 
     150                Hashtable args = null; // buildReturnArgs(); 
    140151                shell.returnFromActivity(this, "Success!", args); 
    141152 
     
    155166                } else if (command.equals(markCommand)) { 
    156167                        processMark(); 
    157                 }   
     168                } 
    158169        } 
    159170 
     
    164175        private void processMark() { 
    165176                // TODO Auto-generated method stub 
    166                  
     177 
    167178        } 
    168179 
    169180        private void processCancel() { 
    170                 shell.returnFromActivity(this, Constants.ACTIVITY_CANCEL, null);                                 
     181                shell.returnFromActivity(this, Constants.ACTIVITY_CANCEL, null); 
    171182        } 
    172183 
    173184        private void processView() { 
    174185                // TODO Auto-generated method stub 
    175                  
     186 
    176187        } 
    177188 
    178189        private void processBrowser() { 
     190                currentKey = FileBrowseActivity.FILE_POINTER; 
    179191                returnFromActivity(new FileBrowseActivity(shell)); 
    180192        } 
    181193 
    182         private void processCamera()  
    183         { 
     194        private void processCamera() { 
    184195                currentKey = ImageCaptureActivity.IMAGE_KEY; 
    185196                returnFromActivity(new ImageCaptureActivity(shell)); 
     
    188199        private void returnFromActivity(IActivity activity) { 
    189200                Hashtable returnArgs = buildReturnArgsFromActivity(activity); 
    190                 shell.returnFromActivity(this, Constants.ACTIVITY_NEEDS_RESOLUTION, returnArgs); 
     201                shell.returnFromActivity(this, Constants.ACTIVITY_NEEDS_RESOLUTION, 
     202                                returnArgs); 
    191203        } 
    192204 
     
    197209        } 
    198210 
    199          
    200  
    201211} 
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/model/FileDataPointer.java

    r1419 r1460  
    22 
    33import org.javarosa.core.model.data.IDataPointer; 
     4import org.javarosa.media.image.utilities.FileUtility; 
    45 
    56/** 
     
    1819         
    1920        public byte[] getData() { 
    20                 // TODO read the file from memory 
    21                 return null; 
     21                return FileUtility.getFileData(fileName); 
    2222        } 
    2323 
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/storage/FileMetaData.java

    r1434 r1460  
    88 * 
    99 */ 
    10 public class ImageMetaData  extends MetaDataObject { 
     10public class FileMetaData  extends MetaDataObject { 
    1111 
    1212         
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/storage/FileRMSUtility.java

    r1434 r1460  
    77 
    88/** 
    9  * A (potentially temporary) class for writing an image to RMS 
     9 * A (potentially temporary) class for writing a basic byte[] to RMS 
    1010 * @author Cory Zue 
    1111 * 
    1212 */ 
    13 public class ImageRMSUtility extends RMSUtility { 
     13public class FileRMSUtility extends RMSUtility { 
    1414 
    1515        private Hashtable images; 
    1616         
    17         public ImageRMSUtility(String name) { 
     17        public FileRMSUtility(String name) { 
    1818                super(name, RMSUtility.RMS_TYPE_STANDARD); 
    19                 images = new Hashtable(); 
     19                images = new Hashtable();        
    2020        } 
    2121         
     
    3333        public void saveImage(String fileName, byte[] data) { 
    3434                 
    35                 ImageMetaData md = new ImageMetaData(); 
     35                FileMetaData md = new FileMetaData(); 
    3636                md.setFileName(fileName); 
    3737                this.writeBytesToRMS(data, md); 
  • branches/dev/j2merosa/org.javarosa.media.image/src/org/javarosa/media/image/utilities/FileUtility.java

    r1455 r1460  
    22 
    33import java.io.IOException; 
     4import java.io.InputStream; 
    45import java.io.OutputStream; 
    56import java.util.Enumeration; 
     
    116117        } 
    117118 
     119         
     120        /** 
     121         * Gets file data from the OS 
     122         * @param fileName 
     123         * @return 
     124         */ 
     125        public static byte[] getFileData(String fileName) { 
     126                InputStream fis = null; 
     127                FileConnection file = null; 
     128                boolean isSaved = false; 
     129                try { 
     130                        file = (FileConnection) Connector.open(fileName); 
     131                        int bytesToRead = (int) file.fileSize(); 
     132                        byte[] toReturn = new byte[bytesToRead]; 
     133                        fis = file.openInputStream(); 
     134                        int bytesRead = 0; 
     135                        int blockSize = 1024; 
     136                        while (bytesToRead > bytesRead) { 
     137                                int thisBlock = blockSize; 
     138                                if (bytesToRead - bytesRead < blockSize) { 
     139                                        thisBlock = bytesToRead-bytesRead; 
     140                                } 
     141                                fis.read(toReturn, bytesRead, thisBlock); 
     142                                bytesRead += blockSize; 
     143                        } 
     144                        return toReturn; 
     145                } catch (Exception ex) {                                 
     146                        handleException(ex); 
     147                }  
     148                finally {                
     149                        close(fis); 
     150                        close(file); 
     151                } 
     152                return null; 
     153        } 
     154        private static void close(InputStream stream) { 
     155                try {                                    
     156                        if (stream != null) { 
     157                                stream.close(); 
     158                        } 
     159                } catch (Exception e) { 
     160                } 
     161        } 
     162 
     163 
    118164        private static void close(OutputStream stream) { 
    119165                try {                                    
     
    140186 
    141187         
     188 
     189         
    142190}