Using CameraPreview to save a picture on disk

Tutorials with advanced 'difficulty' and more Lines of Code.

Postby coding_android » Tue Jul 08, 2008 5:30 pm

I have tried this tutorial and it works fine. What should I do to have a "capture" Button below the preview window. I know that it sets directly the content view at the moment . But how could I use that extend SurfaceView called Preview in my Style XML file in order to get the items like buttons or TextViews arranged with the surface view?

I'm really looking forward getting your answers.
User avatar
coding_android
Moderator
Moderator
 
Posts: 76
Joined: Mon May 05, 2008 10:22 am
Location: Germany

Top

Postby square » Thu Jul 31, 2008 4:00 am

coding_android wrote:I have tried this tutorial and it works fine. What should I do to have a "capture" Button below the preview window. I know that it sets directly the content view at the moment . But how could I use that extend SurfaceView called Preview in my Style XML file in order to get the items like buttons or TextViews arranged with the surface view?

I'm really looking forward getting your answers.


Hi there,

I think I've found the solution to your question. I'm also a newbie :P, so I'm not sure if this is the best solution, but it works for me.
The basic idea of my solution is to create a layout in xml which has in the bottom two buttons, a Capture and a Back button. Set this as the content view of your activity and after this add the mPreview on top of it with the addContentView() method.

The xml layout file for me looks something like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:orientation="vertical"
  7.  
  8.         android:layout_width="fill_parent"
  9.  
  10.         android:layout_height="fill_parent"
  11.  
  12.         android:gravity="bottom"
  13.  
  14.         >
  15.  
  16.         <LinearLayout
  17.  
  18.                 android:orientation="horizontal"
  19.  
  20.                 android:layout_width="fill_parent"
  21.  
  22.                 android:layout_height="wrap_content"
  23.  
  24.                 android:layout_weight="0"
  25.  
  26.                 >
  27.  
  28.                 <Button
  29.  
  30.                         android:id="@+id/capture_button"
  31.  
  32.                         android:layout_width="0dip"
  33.  
  34.                         android:layout_height="wrap_content"
  35.  
  36.                         android:text="Capture"
  37.  
  38.                         android:layout_weight="1"
  39.  
  40.                         />
  41.  
  42.                 <Button
  43.  
  44.                         android:id="@+id/back_button"
  45.  
  46.                         android:layout_width="0dip"
  47.  
  48.                         android:layout_height="wrap_content"
  49.  
  50.                         android:text="Back"
  51.  
  52.                         android:layout_weight="1"
  53.  
  54.                         />
  55.  
  56.         </LinearLayout>
  57.  
  58. </LinearLayout>
  59.  
  60.  
  61.  
  62.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


The activity should implement the OnClickListener interface in order to listen to button clicks.
The onCreate method will look something like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         protected void onCreate(Bundle icicle) {
  3.  
  4.                 super.onCreate(icicle);
  5.  
  6.  
  7.  
  8.                 requestWindowFeature(Window.FEATURE_NO_TITLE);
  9.  
  10.  
  11.  
  12.                 getWindow().setFormat(PixelFormat.TRANSLUCENT);
  13.  
  14.  
  15.  
  16.                 mPreview = new Preview(this);
  17.  
  18.        
  19.  
  20.                 setContentView(R.layout.main);
  21.  
  22.                 addContentView(mPreview, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  23.  
  24.                
  25.  
  26.                 ((Button)findViewById(R.id.back_button)).setOnClickListener(this);
  27.  
  28.                 ((Button)findViewById(R.id.capture_button)).setOnClickListener(this);
  29.  
  30.         }
  31.  
  32.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


And finally in the onClick() method you do the saving or the exit from the program.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public void onClick(View view) {
  3.  
  4.                 if (view == findViewById(R.id.back_button)) {
  5.  
  6.                         setResult(RESULT_OK);
  7.  
  8.                         finish();
  9.  
  10.                 }
  11.  
  12.                 else if (view == findViewById(R.id.capture_button)) {
  13.  
  14.                         mPreview.pause();
  15.  
  16.                         takePicture();
  17.  
  18.                         mPreview.resume();
  19.  
  20.                 }
  21.  
  22.         }
  23.  
  24.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Hope it helps.

Regards,
square
square
Freshman
Freshman
 
Posts: 5
Joined: Wed Jul 16, 2008 4:54 am

Postby bavarol » Mon Aug 11, 2008 12:53 pm

Hi,
can anybody paste his code?
I can capture and save an image onto sd card but I don't know why, the next run I get a null pointer because the last saved image are not on sd card or has a size = 0.

I have tried with .png and jpg

I post my code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         void picture(){
  2.  
  3.                 int i = 0;
  4.  
  5.                 CameraDevice camera = CameraDevice.open();
  6.  
  7.                 if (camera != null) {
  8.  
  9.                         Log.i("MyLog", "inside the camera");
  10.  
  11.                         CameraDevice.CaptureParams param = new CameraDevice.CaptureParams();
  12.  
  13.                         param.type = 1; // preview
  14.  
  15.                         param.srcWidth = 1280;
  16.  
  17.                         param.srcHeight = 960;
  18.  
  19.                         param.leftPixel = 0;
  20.  
  21.                         param.topPixel = 0;
  22.  
  23.                         param.outputWidth = 320;
  24.  
  25.                         param.outputHeight = 240;
  26.  
  27.                         param.dataFormat = 2; // RGB_565
  28.  
  29.                         // Capture of Params
  30.  
  31.                         camera.setCaptureParams(param);
  32.  
  33.                        
  34.  
  35.                         Bitmap myPic = Bitmap.createBitmap(320, 240, false);
  36.  
  37.                         Canvas canvas = new Canvas(myPic);
  38.  
  39.                        
  40.  
  41.                         String picture_path=null;
  42.  
  43.                         try {
  44.  
  45.                                
  46.  
  47.                                 // SD-Card
  48.  
  49.                                 File file;
  50.  
  51.                                 do {
  52.  
  53.                                         picture_path="/sdcard/dsc" + i++ + ".jpg";
  54.  
  55.                                         file = new File(picture_path);
  56.  
  57.                                 } while (file.exists());
  58.  
  59.                                 //==================           
  60.  
  61.                                                                
  62.  
  63.                                
  64.  
  65.                                 BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(picture_path));
  66.  
  67.                                
  68.  
  69.                                 camera.capture(canvas);
  70.  
  71.                                 myPic.compress(CompressFormat.PNG, 100, stream);
  72.  
  73.                                 stream.flush();
  74.  
  75.                                 stream.close();
  76.  
  77.                         }catch(Exception e) { Log.e("CAMERAPREVIEW2:picture()...Exception", e.toString()); }
  78.  
  79.                        
  80.  
  81.                         // Release the CameraDevice and Saving of the picture
  82.  
  83.                         if (camera != null) {
  84.  
  85.                                 camera.close();
  86.  
  87.                                 savePicture(picture_path);
  88.  
  89.                         }      
  90.  
  91.                 }
  92.  
  93.         }
  94.  
  95.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


The method savePicture put the parameters, which a subactivity uses tu edit the description
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4.     void savePicture(String picture_path) {
  5.  
  6.        
  7.  
  8.         Intent intent = new Intent(CameraPreview2.this, EditPhoto.class);      
  9.  
  10.         /* Create a bundle that will hold will be passed to the
  11.  
  12.          * SubActivity over the Intent */
  13.  
  14.        
  15.  
  16.         Bundle bundle = new Bundle();
  17.  
  18.        
  19.  
  20.         bundle.putString(TourGuide.DESCRIPTION, "");
  21.  
  22.         bundle.putString(TourGuide.PICTURE_PATH, picture_path);
  23.  
  24.         bundle.putInteger(TourGuide.LATITUDE, latitude);
  25.  
  26.         bundle.putInteger(TourGuide.LONGITUDE, longitude);
  27.  
  28.         bundle.putLong(TourGuide._ID, _id);
  29.  
  30.         intent.putExtras(bundle);
  31.  
  32.                
  33.  
  34.         Log.i("CAMERAPREVIEW2:savePicture()",
  35.  
  36.                                 "picture_path: "+picture_path+
  37.  
  38.                                 "latitude: "+ latitude+
  39.  
  40.                                 "longitude: "+ longitude+
  41.  
  42.                                 "_id: "+_id);
  43.  
  44.        
  45.  
  46.         startSubActivity(intent, CREATE_CODE);
  47.  
  48.        
  49.  
  50.        
  51.  
  52.     }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


What am I doing wrong?
Why dissapears the last image?

I have tried to decode de Bitmap with BitmapFactory.decode(picture_path) and with a BitmapFactory.decodeStream(inputStream) with the proper callings but it dissapears the last image or it will be reset .

Thx in advance

p.s. My SDK is m3rc22a
bavarol
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Mon Dec 10, 2007 9:19 pm

Postby sm12 » Thu Nov 13, 2008 4:52 am

Is this topic still active?

I'm desparately trying to finish this tutorial, but so far unlucky.
The problem is with compatibility - I'm using latest version of SDK (1.0). Since lots of thing have changed the code provided does not work anymore.

I'm strugling at
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. void takePicture()
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
method, where we need to set parameters.
CameraDevice, CameraDevice.Params, and many other classes were removed and consequently methods have changed.

Could you please update this tutorial or refer to other similar one.

Thank you.
sm12
Freshman
Freshman
 
Posts: 7
Joined: Thu Nov 13, 2008 4:36 am

CameraDevice class is skipped

Postby chetandl » Thu Mar 19, 2009 9:41 am

Hello ,

In new sdk CameraDevice class is skipped.
Does anyone have a good example with new SDK
chetandl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Mar 17, 2009 9:17 am

How to put camera preview inside an activity view?

Postby tinyang » Thu Aug 27, 2009 12:13 am

Here is my code, but it does not get the cam preview inside my activity view. Can anyone please help me to understand what is wrong?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. protected void onCreate(Bundle savedInstanceState) {
  3.  
  4.         super.onCreate(savedInstanceState);
  5.  
  6.         camPreview = new Preview(this); //create preview
  7.  
  8.         setContentView(R.layout.takepic);
  9.  
  10.         setContentView(camPreview); //set preview as activity content
  11.  
  12.         camSurface = (SurfaceView) findViewById(R.id.camsurface);
  13.  
  14.         }
  15.  
  16.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
--
Moderation is the key to happiness and harmony in one's life.
User avatar
tinyang
Freshman
Freshman
 
Posts: 6
Joined: Sat Jan 03, 2009 4:50 pm

Top

Postby piXi » Fri Dec 04, 2009 11:00 am

Hi all,

I'm trying to develop CameraPreview to take photo directly from camera.

At first I had a problem with black screen at SurfaceView but i solved that.
Now i have problem when showing preview, the image is not clear, instead some strange vertical
lines are running, that you cant recognice image previewed.

After taking picture clear image is shown.

What could cause those strange vertical lines ?

Im using HTC Magic with 1.5 sdk.

in surfaceCreated im calling

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. camera = Camera.open();                        
  3.  
  4. try {
  5.  
  6.         camera.setPreviewDisplay(holder);
  7.  
  8. } catch (IOException e) {
  9.  
  10.         e.printStackTrace();
  11.  
  12. }
  13.  
  14.  
  15.  
  16.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


UPDATE:

just tryed on SamsungGalaxy where preview is shown normal :S.
piXi
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Dec 04, 2009 10:50 am

Postby eyurdakul » Sun Mar 07, 2010 12:04 am

piXi, the topic is quiet old and you've probably found another work around. But the cause of your problem is, preview format. Try to add this in your code, or whoever struggles with the same problem;

Parameters params = camera.getParameters();
params.setPreviewFormat(PixelFormat.JPEG);
eyurdakul
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Mar 04, 2010 1:19 pm

Re: Using CameraPreview to save a picture on disk

Postby sumit_android » Tue Jun 29, 2010 11:54 am

hiii this code in not run in my eclipse.
CameraDevice & CameraDevice.CaptureParams is what.....
code not cleared this two things.
sumit_android
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Feb 19, 2010 6:59 am
Location: India

Re: Using CameraPreview to save a picture on disk

Postby sumit_android » Wed Jul 21, 2010 11:45 am

hiiiii carl...
where you attached your code.....
i am unable to get it here...
can you plz put that code as vankat did..
really its very urgent for me..so plz send ur code here as quick as possible.
sumit_android
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Feb 19, 2010 6:59 am
Location: India

Re: Using CameraPreview to save a picture on disk

Postby edreamz » Fri Sep 17, 2010 4:12 pm

Can anyone help me get the updated code for the 2.2 platform. I think the whole code needs a re-vamp....
User avatar
edreamz
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Aug 06, 2010 2:29 am
Location: U.S

Re: Using CameraPreview to save a picture on disk

Postby blacklisttom07 » Wed Jun 29, 2011 8:10 am

Hi,I'm new here.
This forum is very interesting that I like it very much.
blacklisttom07
Freshman
Freshman
 
Posts: 6
Joined: Wed Jun 29, 2011 7:39 am

Re: Using CameraPreview to save a picture on disk

Postby Openxcell » Sat Dec 17, 2011 7:42 am

I think android 2.2 would be same as android 2.3 and no any more changes has been done on these camera preview project. It would be good that you must change the manifest file with min SDK version of what that android SDK 2.2.
Openxcell
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Mon Mar 28, 2011 1:59 pm

Re: Using CameraPreview to save a picture on disk

Postby SusanGray » Mon Mar 11, 2013 12:57 pm

Code: Select all
pre.camera.takePicture(shutterCallback, rawCallback,
                            jpegCallback);
            PictureCallback rawCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                System.out.println( "onPictureTaken - raw");
            }
        };

        /** Handles data for jpeg picture */
        PictureCallback jpegCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {


                 BitmapFactory.Options options=new BitmapFactory.Options();
                    options.inSampleSize = 5;

                m=BitmapFactory.decodeByteArray(data,0,data.length,options);
SocialCubix is the Android Development Agency
SusanGray
Freshman
Freshman
 
Posts: 3
Joined: Mon Mar 11, 2013 12:39 pm
Location: United States

Top
Previous

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests