Take Picture from Camera Emulator

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Take Picture from Camera Emulator

Postby venkat » Sat Dec 01, 2007 2:42 pm

Can u any one tell me how to take picture from camera emulator and save that picture to disk, And it's possible to change camera emulator view??? :?:

Thanks in advance.

regards,
venkat
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Top

Postby faldureon » Sun Dec 09, 2007 10:30 pm

Here is a simple activity that shows "preview" pictures that refresh every second, and lets you take a bigger final picture via a menu item and save it. Just create a new android project and put this activity into it.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.technocore.ui;
  2.  
  3.  
  4.  
  5. import java.io.FileOutputStream;
  6.  
  7. import java.io.IOException;
  8.  
  9. import java.util.Random;
  10.  
  11.  
  12.  
  13. import android.app.Activity;
  14.  
  15. import android.content.Context;
  16.  
  17. import android.graphics.Bitmap;
  18.  
  19. import android.graphics.Canvas;
  20.  
  21. import android.graphics.Color;
  22.  
  23. import android.graphics.Paint;
  24.  
  25. import android.graphics.Rect;
  26.  
  27. import android.graphics.Bitmap.CompressFormat;
  28.  
  29. import android.hardware.CameraDevice;
  30.  
  31. import android.hardware.CameraDevice.CaptureParams;
  32.  
  33. import android.os.Bundle;
  34.  
  35. import android.os.Handler;
  36.  
  37. import android.view.Menu;
  38.  
  39. import android.widget.ImageView;
  40.  
  41.  
  42.  
  43. public class PicuteCapturer extends Activity implements Runnable {
  44.  
  45.         /**
  46.  
  47.          * name of the file into which the image gets saved
  48.  
  49.          */
  50.  
  51.         public static final String NAME_OF_IMAGE_FILE = "newPic.png";
  52.  
  53.  
  54.  
  55.         public static final int PREVIEW_IMAGE_HEIGHT = 240;
  56.  
  57.         public static final int PREVIEW_IMAGE_WIDTH = 320;
  58.  
  59.  
  60.  
  61.         public static final int FULL_IMAGE_HEIGHT = 1024;
  62.  
  63.         public static final int FULL_IMAGE_WIDTH = 1280;
  64.  
  65.  
  66.  
  67.         /**
  68.  
  69.          * Holds menu item names and ids
  70.  
  71.          */
  72.  
  73.         enum MenuItems {
  74.  
  75.                 TAKE_PICTURE("Take Picture"), SAVE_PICTURE("Save picture"), RESUME_PREVIEW(
  76.  
  77.                                 "Resume preview"), GO_BACK("Go back");
  78.  
  79.  
  80.  
  81.                 String menuItemText;
  82.  
  83.                 int id;
  84.  
  85.  
  86.  
  87.                 MenuItems(String menuItemText) {
  88.  
  89.                         this.menuItemText = menuItemText;
  90.  
  91.                         // create a unique id by using the name hash as a seed for the
  92.  
  93.                         // random
  94.  
  95.                         // number generator
  96.  
  97.                         id = (new Random(name().hashCode())).nextInt();
  98.  
  99.                 }
  100.  
  101.  
  102.  
  103.                 public String getMenuItemText() {
  104.  
  105.                         return menuItemText;
  106.  
  107.                 }
  108.  
  109.  
  110.  
  111.                 public int getId() {
  112.  
  113.                         return id;
  114.  
  115.                 }
  116.  
  117.         }
  118.  
  119.  
  120.  
  121.         /**
  122.  
  123.          * boolean that stops the preview thread
  124.  
  125.          */
  126.  
  127.         private boolean mTrucking = false;
  128.  
  129.  
  130.  
  131.         /**
  132.  
  133.          * time between calls to CameraDevice.capture during preview
  134.  
  135.          */
  136.  
  137.         private long refreshRateInMilliSeconds = 1000;
  138.  
  139.  
  140.  
  141.         /**
  142.  
  143.          * counter for the image stamp
  144.  
  145.          */
  146.  
  147.         private int counter = 1;
  148.  
  149.  
  150.  
  151.         /**
  152.  
  153.          * the main view that holds either a preview image or a final image
  154.  
  155.          */
  156.  
  157.         private ImageView imageView;
  158.  
  159.  
  160.  
  161.         /**
  162.  
  163.          * This takes the pictures
  164.  
  165.          */
  166.  
  167.         private CameraDevice cameraDevice;
  168.  
  169.  
  170.  
  171.         /**
  172.  
  173.          * handler for telling the ui thread that its time to update the preview
  174.  
  175.          * picture
  176.  
  177.          */
  178.  
  179.         private Handler handler;
  180.  
  181.  
  182.  
  183.         /**
  184.  
  185.          * since menu items get shown and hidden depending on the application state
  186.  
  187.          * we need to keep a reference to the menu
  188.  
  189.          */
  190.  
  191.         private Menu menu;
  192.  
  193.  
  194.  
  195.         /**
  196.  
  197.          * The imageview does not provide an easy way to get the bitmap out of it,
  198.  
  199.          * so we hold on to it in case the user decides to save it
  200.  
  201.          */
  202.  
  203.         private Bitmap bitmapToSave = null;
  204.  
  205.  
  206.  
  207.         @Override
  208.  
  209.         protected void onCreate(Bundle icicle) {
  210.  
  211.                 super.onCreate(icicle);
  212.  
  213.  
  214.  
  215.                 initBitMaps();
  216.  
  217.  
  218.  
  219.                 // initialise the handler and the view
  220.  
  221.                 handler = new Handler();
  222.  
  223.                 imageView = new ImageView(this);
  224.  
  225.  
  226.  
  227.                 setContentView(imageView);
  228.  
  229.         }
  230.  
  231.  
  232.  
  233.         final Runnable mUpdateResults = new Runnable() {
  234.  
  235.                 public void run() {
  236.  
  237.                         // this happens whenever its time to update the preview image
  238.  
  239.                         imageView.setImageBitmap(captureImage(true));
  240.  
  241.                 }
  242.  
  243.         };
  244.  
  245.  
  246.  
  247.         private Bitmap previewBitmap;
  248.  
  249.  
  250.  
  251.         private Bitmap fullBitmap;
  252.  
  253.  
  254.  
  255.         @Override
  256.  
  257.         protected void onResume() {
  258.  
  259.                 super.onResume();
  260.  
  261.  
  262.  
  263.                 // once the application is resumed init the camera device and start the
  264.  
  265.                 // preview
  266.  
  267.                 cameraDevice = CameraDevice.open();
  268.  
  269.                 startPreview();
  270.  
  271.         }
  272.  
  273.  
  274.  
  275.         @Override
  276.  
  277.         protected void onPause() {
  278.  
  279.                 super.onPause();
  280.  
  281.  
  282.  
  283.                 // close the camera device and stop the preview thread.
  284.  
  285.                 stopPreview();
  286.  
  287.                 cameraDevice.close();
  288.  
  289.         }
  290.  
  291.  
  292.  
  293.         private void startPreview() {
  294.  
  295.                 // only start one thread
  296.  
  297.                 if (!mTrucking) {
  298.  
  299.                         mTrucking = true;
  300.  
  301.                         Thread thread = new Thread(this);
  302.  
  303.                         thread.start();
  304.  
  305.                 }
  306.  
  307.         }
  308.  
  309.  
  310.  
  311.         private void stopPreview() {
  312.  
  313.                 // setting mTrucking to false ends the preview thread
  314.  
  315.                 mTrucking = false;
  316.  
  317.         }
  318.  
  319.  
  320.  
  321.         @Override
  322.  
  323.         public boolean onCreateOptionsMenu(Menu menu) {
  324.  
  325.                 boolean supRetVal = super.onCreateOptionsMenu(menu);
  326.  
  327.                 this.menu = menu;
  328.  
  329.                 menu.add(0, MenuItems.TAKE_PICTURE.getId(), MenuItems.TAKE_PICTURE
  330.  
  331.                                 .getMenuItemText());
  332.  
  333.                 menu.add(0, MenuItems.SAVE_PICTURE.getId(), MenuItems.SAVE_PICTURE
  334.  
  335.                                 .getMenuItemText());
  336.  
  337.                 menu.add(0, MenuItems.RESUME_PREVIEW.getId(), MenuItems.RESUME_PREVIEW
  338.  
  339.                                 .getMenuItemText());
  340.  
  341.                 menu.add(0, MenuItems.GO_BACK.getId(), MenuItems.GO_BACK
  342.  
  343.                                 .getMenuItemText());
  344.  
  345.  
  346.  
  347.                 // it doesn't make sense to let users save preview images or resume
  348.  
  349.                 // preview while previewing so hide those menu items
  350.  
  351.                 menu.setItemShown(MenuItems.SAVE_PICTURE.getId(), false);
  352.  
  353.                 menu.setItemShown(MenuItems.RESUME_PREVIEW.getId(), false);
  354.  
  355.                 return supRetVal;
  356.  
  357.         }
  358.  
  359.  
  360.  
  361.         @Override
  362.  
  363.         public boolean onOptionsItemSelected(Menu.Item item) {
  364.  
  365.                 if (item.getId() == MenuItems.TAKE_PICTURE.getId()) {
  366.  
  367.                         // stop preview
  368.  
  369.                         stopPreview();
  370.  
  371.  
  372.  
  373.                         // take the big picture
  374.  
  375.                         bitmapToSave = captureImage(false);
  376.  
  377.  
  378.  
  379.                         // set it on the view
  380.  
  381.                         imageView.setImageBitmap(bitmapToSave);
  382.  
  383.  
  384.  
  385.                         // enable menu items
  386.  
  387.                         menu.setItemShown(MenuItems.SAVE_PICTURE.getId(), true);
  388.  
  389.                         menu.setItemShown(MenuItems.RESUME_PREVIEW.getId(), true);
  390.  
  391.                         return true;
  392.  
  393.                 } else if (item.getId() == MenuItems.SAVE_PICTURE.getId()) {
  394.  
  395.                         saveImage();
  396.  
  397.                         return true;
  398.  
  399.                 } else if (item.getId() == MenuItems.RESUME_PREVIEW.getId()) {
  400.  
  401.                         menu.setItemShown(MenuItems.SAVE_PICTURE.getId(), false);
  402.  
  403.                         menu.setItemShown(MenuItems.RESUME_PREVIEW.getId(), false);
  404.  
  405.                         startPreview();
  406.  
  407.                         return true;
  408.  
  409.                 } else if (item.getId() == MenuItems.GO_BACK.getId()) {
  410.  
  411.                         stopPreview();
  412.  
  413.                         finish();
  414.  
  415.                         return true;
  416.  
  417.                 }
  418.  
  419.  
  420.  
  421.                 return false;
  422.  
  423.         }
  424.  
  425.  
  426.  
  427.         private void saveImage() {
  428.  
  429.                 FileOutputStream fileOutputStream = null;
  430.  
  431.                 try {
  432.  
  433.                         // let everyone access the created image
  434.  
  435.                         fileOutputStream = this.openFileOutput(NAME_OF_IMAGE_FILE,
  436.  
  437.                                         Context.MODE_WORLD_READABLE);
  438.  
  439.                         bitmapToSave.compress(CompressFormat.PNG, 100, fileOutputStream);
  440.  
  441.                 } catch (IOException e) {
  442.  
  443.                         showAlert("Failed!", "Couldn't save image " + NAME_OF_IMAGE_FILE,
  444.  
  445.                                         "ok", false);
  446.  
  447.                 } finally {
  448.  
  449.                         try {
  450.  
  451.                                 fileOutputStream.close();
  452.  
  453.                         } catch (IOException e) {
  454.  
  455.                                 // do nothing
  456.  
  457.                         }
  458.  
  459.                 }
  460.  
  461.  
  462.  
  463.         }
  464.  
  465.  
  466.  
  467.         private void initBitMaps() {
  468.  
  469.                 previewBitmap = Bitmap.createBitmap(PREVIEW_IMAGE_WIDTH,
  470.  
  471.                                 PREVIEW_IMAGE_HEIGHT, true);
  472.  
  473.                 fullBitmap = Bitmap.createBitmap(FULL_IMAGE_WIDTH, FULL_IMAGE_HEIGHT,
  474.  
  475.                                 true);
  476.  
  477.         }
  478.  
  479.  
  480.  
  481.         private Bitmap captureImage(boolean preview) {
  482.  
  483.                 int width = -1;
  484.  
  485.                 int height = -1;
  486.  
  487.                 Canvas canvas = null;
  488.  
  489.                 Bitmap bitmapToUse = null;
  490.  
  491.                 CaptureParams cp = new CameraDevice.CaptureParams();
  492.  
  493.                 if (preview) {
  494.  
  495.                         width = PREVIEW_IMAGE_WIDTH;
  496.  
  497.                         height = PREVIEW_IMAGE_HEIGHT;
  498.  
  499.                         bitmapToUse = previewBitmap;
  500.  
  501.                         cp.type = 1;
  502.  
  503.                 } else {
  504.  
  505.                         width = FULL_IMAGE_WIDTH;
  506.  
  507.                         height = FULL_IMAGE_HEIGHT;
  508.  
  509.                         bitmapToUse = fullBitmap;
  510.  
  511.                         cp.type = 0;
  512.  
  513.                 }
  514.  
  515.                 cp.srcHeight = 1024;
  516.  
  517.                 cp.srcWidth = 1280;
  518.  
  519.                 cp.outputHeight = height;
  520.  
  521.                 cp.outputWidth = width;
  522.  
  523.                 cp.dataFormat = 2;
  524.  
  525.  
  526.  
  527.                 canvas = new Canvas(bitmapToUse);
  528.  
  529.  
  530.  
  531.                 // paint the canvas white, then use a black font
  532.  
  533.                 Paint paint = new Paint();
  534.  
  535.                 paint.setColor(Color.WHITE);
  536.  
  537.                 canvas.drawRect(new Rect(0, 0, width, height), paint);
  538.  
  539.                 if (cameraDevice != null) {
  540.  
  541.                         cameraDevice.setCaptureParams(cp);
  542.  
  543.                         if (cameraDevice.capture(canvas)) {
  544.  
  545.                                 paint.setTextSize(25);
  546.  
  547.                                 paint.setColor(Color.BLACK);
  548.  
  549.                                 counter++;
  550.  
  551.                                 // stamp the image so changes are apperent
  552.  
  553.                                 canvas.drawText("MOOOO" + counter, 50, 50, paint);
  554.  
  555.                         }
  556.  
  557.                 }
  558.  
  559.  
  560.  
  561.                 return bitmapToUse;
  562.  
  563.         }
  564.  
  565.  
  566.  
  567.         @Override
  568.  
  569.         public void run() {
  570.  
  571.                 while (mTrucking) {
  572.  
  573.                         // let the ui thread know its time to update the preview image
  574.  
  575.                         handler.post(mUpdateResults);
  576.  
  577.                         try {
  578.  
  579.                                 Thread.sleep(refreshRateInMilliSeconds);
  580.  
  581.                         } catch (InterruptedException e) {
  582.  
  583.                                 // do nothing
  584.  
  585.                         }
  586.  
  587.                 }
  588.  
  589.  
  590.  
  591.         }
  592.  
  593.  
  594.  
  595. }
Parsed in 0.061 seconds, using GeSHi 1.0.8.4
faldureon
Developer
Developer
 
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm

Ooops

Postby faldureon » Tue Dec 11, 2007 3:08 am

I had versioning system malfunction and pasted the wrong chunk of code! Sorry about that, here is the one that actually works:

:cut: :cut: :cut: :cut: :cut:

Edit by plusminus:
Replace the working code with the "bad" code above.
faldureon
Developer
Developer
 
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm

Postby misterbo » Sat Mar 08, 2008 12:36 am

Thanks a lot for this tutorial!

Where is the image file being stored though when I want to access it at a later point with FileInputStream(fileName) ...? I have trouble finding the right path. :(

Cheers,
boris
misterbo
Junior Developer
Junior Developer
 
Posts: 10
Joined: Mon Dec 31, 2007 4:13 pm

Postby plusminus » Sat Mar 08, 2008 4:48 pm

Hello boris,

this is how the file is saved:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                fileOutputStream = this.openFileOutput(NAME_OF_IMAGE_FILE,
  2.  
  3.                          Context.MODE_WORLD_READABLE);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

The file is now located in the following folder on your emulator:
[align=center]"/data/data/your_project_package_structure/files/bla.png"[/align]
To load it back, use:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FileInputStream fIn = openFileInput(NAME_OF_IMAGE_FILE); // aka "bla.png"
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


:idea: More info: http://www.anddev.org/viewtopic.php?t=115

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby haitian » Wed Mar 19, 2008 8:49 am

faldureon wrote:Here is a simple activity that shows "preview" pictures that refresh every second, and lets you take a bigger final picture via a menu item and save it. Just create a new android project and put this activity into it.


why does it look like this? The picture can't take up all the canvas.
Attachments
Snap1.jpg
Snap1.jpg (14.98 KiB) Viewed 41819 times
haitian
Freshman
Freshman
 
Posts: 4
Joined: Tue Jan 22, 2008 9:33 am

Top

Postby faldureon » Wed Mar 19, 2008 4:55 pm

I hard coded the size of the picture into the example
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    public static final int PREVIEW_IMAGE_HEIGHT = 240;
  2.  
  3.      public static final int PREVIEW_IMAGE_WIDTH = 320;
  4.  
  5.  
  6.  
  7.      public static final int FULL_IMAGE_HEIGHT = 1024;
  8.  
  9.      public static final int FULL_IMAGE_WIDTH = 1280;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

You can change those to be equal to your emulator screen dimensions to make the picture take up the entire screen
faldureon
Developer
Developer
 
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm

Postby haitian » Mon Mar 24, 2008 3:37 am

faldureon wrote:I hard coded the size of the picture into the example
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    public static final int PREVIEW_IMAGE_HEIGHT = 240;
  2.      public static final int PREVIEW_IMAGE_WIDTH = 320;
  3.  
  4.      public static final int FULL_IMAGE_HEIGHT = 1024;
  5.      public static final int FULL_IMAGE_WIDTH = 1280;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

You can change those to be equal to your emulator screen dimensions to make the picture take up the entire screen


First, I set the emulator screen size to QVGA-L

Second, I change
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public static final int PREVIEW_IMAGE_HEIGHT = 240;
  3.  
  4.         public static final int PREVIEW_IMAGE_WIDTH = 320;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

to
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public static int PREVIEW_IMAGE_HEIGHT = 240;
  3.  
  4.         public static int PREVIEW_IMAGE_WIDTH = 320;
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Last, In onCreate() method, before initBitMaps() I add

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         DisplayMetrics dm = new DisplayMetrics();
  3.  
  4.         getWindowManager().getDefaultDisplay().getMetrics(dm);
  5.  
  6.         PREVIEW_IMAGE_HEIGHT = dm.heightPixels;
  7.  
  8.         PREVIEW_IMAGE_WIDTH = dm.widthPixels;
  9.  
  10.        
  11.  
  12.         initBitMaps();
  13.  
  14.        
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


But, the preview still can't take up the entire screen. :(
haitian
Freshman
Freshman
 
Posts: 4
Joined: Tue Jan 22, 2008 9:33 am

Android Camera

Postby Shazia » Thu Nov 20, 2008 11:35 am

1.Can u plz provide the code for capturing the image from Android camera for the version 1.0.
2.Is there any way to work with OCR using Android??????
Shazia
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 20, 2008 11:23 am

Camera Capture for SDK 1r1

Postby java.padawan » Sun Nov 23, 2008 6:48 pm

Hi, I just created/compiled all source codes from various sources to make my own camera capture.

Android Camera Capture

Although, it works, I encountered some problems which I am not sure whether its a bug or I did something wrong.
java.padawan
Freshman
Freshman
 
Posts: 6
Joined: Mon Oct 27, 2008 7:07 pm

Errors

Postby chetandl » Tue Mar 17, 2009 1:35 pm

Hi ,

I get errors while compiling your code to capture image code do you have complete eclipse project
chetandl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Mar 17, 2009 9:17 am

Top

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests