andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Using CameraPreview to save a picture on disk

Goto page Previous  1, 2, 3
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Advanced Tutorials
Author Message
coding_android
Moderator
Moderator


Joined: 05 May 2008
Posts: 63
Location: Germany

PostPosted: Tue Jul 08, 2008 5:30 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
square
Freshman
Freshman


Joined: 16 Jul 2008
Posts: 5

PostPosted: Thu Jul 31, 2008 4:00 am    Post subject: Reply with quote

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 Razz, 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:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:gravity="bottom"
     >

     <LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="0"
          >

          <Button
               android:id="@+id/capture_button"
               android:layout_width="0dip"
               android:layout_height="wrap_content"
               android:text="Capture"
               android:layout_weight="1"
               />

          <Button
               android:id="@+id/back_button"
               android:layout_width="0dip"
               android:layout_height="wrap_content"
               android:text="Back"
               android:layout_weight="1"
               />

     </LinearLayout>
</LinearLayout>



The activity should implement the OnClickListener interface in order to listen to button clicks.
The onCreate method will look something like this:
Java:

     protected void onCreate(Bundle icicle) {
          super.onCreate(icicle);

          requestWindowFeature(Window.FEATURE_NO_TITLE);

          getWindow().setFormat(PixelFormat.TRANSLUCENT);

          mPreview = new Preview(this);
     
          setContentView(R.layout.main);
          addContentView(mPreview, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          
          ((Button)findViewById(R.id.back_button)).setOnClickListener(this);
          ((Button)findViewById(R.id.capture_button)).setOnClickListener(this);
     }


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

Java:

     public void onClick(View view) {
          if (view == findViewById(R.id.back_button)) {
               setResult(RESULT_OK);
               finish();
          }
          else if (view == findViewById(R.id.capture_button)) {
               mPreview.pause();
               takePicture();
               mPreview.resume();
          }
     }


Hope it helps.

Regards,
square
Back to top
View user's profile Send private message
bavarol
Experienced Developer
Experienced Developer


Joined: 10 Dec 2007
Posts: 52

PostPosted: Mon Aug 11, 2008 12:53 pm    Post subject: Reply with quote

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

Java:
     void picture(){
          int i = 0;
          CameraDevice camera = CameraDevice.open();
          if (camera != null) {
               Log.i("MyLog", "inside the camera");
               CameraDevice.CaptureParams param = new CameraDevice.CaptureParams();
               param.type = 1; // preview
               param.srcWidth = 1280;
               param.srcHeight = 960;
               param.leftPixel = 0;
               param.topPixel = 0;
               param.outputWidth = 320;
               param.outputHeight = 240;
               param.dataFormat = 2; // RGB_565
               // Capture of Params
               camera.setCaptureParams(param);
               
               Bitmap myPic = Bitmap.createBitmap(320, 240, false);
               Canvas canvas = new Canvas(myPic);
               
               String picture_path=null;
               try {
                    
                    // SD-Card
                    File file;
                    do {
                         picture_path="/sdcard/dsc" + i++ + ".jpg";
                         file = new File(picture_path);
                    } while (file.exists());
                    //==================          
                                        
                    
                    BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(picture_path));
                    
                    camera.capture(canvas);
                    myPic.compress(CompressFormat.PNG, 100, stream);
                    stream.flush();
                    stream.close();
               }catch(Exception e) { Log.e("CAMERAPREVIEW2:picture()...Exception", e.toString()); }
               
               // Release the CameraDevice and Saving of the picture
               if (camera != null) {
                    camera.close();
                    savePicture(picture_path);
               }   
          }
     }


The method savePicture put the parameters, which a subactivity uses tu edit the description
Java:


    void savePicture(String picture_path) {
     
     Intent intent = new Intent(CameraPreview2.this, EditPhoto.class);     
     /* Create a bundle that will hold will be passed to the
      * SubActivity over the Intent */

     
     Bundle bundle = new Bundle();
     
     bundle.putString(TourGuide.DESCRIPTION, "");
     bundle.putString(TourGuide.PICTURE_PATH, picture_path);
     bundle.putInteger(TourGuide.LATITUDE, latitude);
     bundle.putInteger(TourGuide.LONGITUDE, longitude);
     bundle.putLong(TourGuide._ID, _id);
     intent.putExtras(bundle);
          
     Log.i("CAMERAPREVIEW2:savePicture()",
                    "picture_path: "+picture_path+
                    "latitude: "+ latitude+
                    "longitude: "+ longitude+
                    "_id: "+_id);
     
     startSubActivity(intent, CREATE_CODE);
     
       
    }


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
Back to top
View user's profile Send private message
sm12
Freshman
Freshman


Joined: 13 Nov 2008
Posts: 3

PostPosted: Thu Nov 13, 2008 4:52 am    Post subject: Reply with quote

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
Java:
void takePicture()
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.
Back to top
View user's profile Send private message
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Advanced Tutorials All times are GMT + 1 Hour
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.