Camera Preview

by roscoe_x » Mon, 15 Jun 2009 04:35:19 GMT


Sponsored Links
 Camera Preview sample code in here:
 http://developer.android.com/guide/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html 

My questions are:
1. There's an error in the program, in the try{} catch{} part in
surfaceCreated(). Any suggestions on how to solve this problem?
2. In the top part of the html page written "The file containing the
source code shown below is located in the corresponding directory in
<sdk>/platforms/android-<version>/samples/...". But I can not find the
sample code in that directory. Is the information wrong or my system
is incomplete?
3. What do I get (see) when the program run normally?

Thanks for the answer.

--~--~---------~--~----~------------~-------~--~----~



Camera Preview

by pat » Wed, 17 Jun 2009 01:49:36 GMT


 Hi everybody,

I implemented a camera preview SurfaceView that is loosely based on
the camera preview example from the android apis demo.

On the same activity, i have some live sensor readings displayed.

I always had the idea that the activity is running very slow and i
previously got some image freezings when I switched between two
different activities that both displayed the camera preview. That was
when I was building for SDK 1.1 today I migrated my application to 1.5
and catched the following error message:

06-15 23:56:26.272: ERROR/AndroidRuntime(726):
java.lang.RuntimeException: Out of memory
06-15 23:56:26.272: ERROR/AndroidRuntime(726):     at
android.hardware.Camera.native_setup(Native Method)
06-15 23:56:26.272: ERROR/AndroidRuntime(726):     at
android.hardware.Camera.<init>(Camera.java:82)
06-15 23:56:26.272: ERROR/AndroidRuntime(726):     at
android.hardware.Camera.open(Camera.java:64)

For clarity, I will post the code of my Preview class at the end.

Any idea what could be wrong?

Thanks a lot for your replies.

Patrick




package nl.uva.graffiti;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public classPreview extends SurfaceView implements
SurfaceHolder.Callback{

    SurfaceHolder mHolder;
    Camera mCamera;
    Context mContext;

    public Preview(Context context) {
        super(context);
        init();
    }

    public Preview (Context context, AttributeSet attrs){
        super(context, attrs);
        mContext = context;
        init();
    }

    public Preview(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();
        try {
           mCamera.setPreviewDisplay(holder);
        } catch (Exception exception) {
            mCamera.release();
            mCamera = null;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int
w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

}

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Camera preview

by tinyang » Fri, 28 Aug 2009 03:43:28 GMT


 Hello.

I just got my camera preview working for my app, but it is not
appearing
where I want it to appear, and I'm not sure how to get it there.
Instead of
using the entire screen for the preview, I would like to put it inside
a
surfaceview in an activity xml gui.  What do I need to change?  Here
is my
code:

public class TakePic extends Activity {

SurfaceView camSurface;

Preview camPreview;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);//hide window title

camPreview = new Preview(this); //create preview

setContentView(R.layout.takepic);

setContentView(camPreview); //set preview as activity content

camSurface = (SurfaceView) findViewById(R.id.camsurface);

}
--~--~---------~--~----~------------~-------~--~----~



Camera preview

by Pavel » Fri, 28 Aug 2009 16:00:12 GMT


 Hey tinyang,

Could you please share the code you use for picture taking? I am
working on the same thing and having weird issues with it - it does
not take full sized pictures for unknown reason...

And yeah, sorry, no answer to your question from my side at the moment.

Pavlo






--~--~---------~--~----~------------~-------~--~----~



Camera preview

by tinyang » Sat, 29 Aug 2009 03:50:52 GMT


 Hi Pavel, and thanks for the reply.  I'm happy to share the snippet of
code I have for picture taking, but I have not tried it out yet
because in order to see and use the picture-taking button in my
activity view, I need to get the camera preview in the correct place
first.

I still really need someone to help me on this please!! :)

Here is the code I was looking at trying, but I've done extensive
research on the subject trying to fix my placement of the camera
preview, so here are also some links that may help you with taking the
picture and storing it properly: (HTH!)

 http://www.devx.com/SpecialReports/Article/37721/1763?supportItem=5 

 http://www.devx.com/SpecialReports/Article/37721/1763? 

 http://www.anddev.org/viewtopic.php?p=704 #704

 http://www.androidph.com/2008/11/camera-capture.html 

 http://www.devx.com/SpecialReports/Article/37721/1954 

 http://www.cs.trincoll.edu/hfoss/wiki/Using_the_Android_camera 

Code:
public class ImageCaptureCallback implements PictureCallback  {

            private OutputStream filoutputStream;
            public ImageCaptureCallback(OutputStream filoutputStream) {
                this.filoutputStream = filoutputStream;
            }
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                try {
                        Log.v(getClass().getSimpleName(), "onPictureTaken=" + 
data + "
length = " + data.length);
                        filoutputStream.write(data);
                        filoutputStream.flush();
                        filoutputStream.close();
                        } catch(Exception ex) {
                                ex.printStackTrace();
                        }
                }
            }
}





--~--~---------~--~----~------------~-------~--~----~



Camera preview

by Bill » Sat, 29 Aug 2009 09:23:13 GMT


 There seems to be a bug in the EXTRA_OPTIONS if you're using the
default camera activity. That is why you are not getting full sized
pictures.

If you want to embed the preview you can use the Camera object
directly.





--~--~---------~--~----~------------~-------~--~----~



Camera preview

by tinyang » Mon, 31 Aug 2009 21:42:55 GMT


 Thank you Bill for your response!  This has been confounding me for a
while and I've had no idea how to remedy it.  Is it possible for you
to please include a code sample or a link to a code sample?





--~--~---------~--~----~------------~-------~--~----~



Re: Camera preview

by Mark Murphy » Fri, 11 Feb 2011 20:20:56 GMT


 That example has a flaw -- it hard-wires in /sdcard as the location of
external storage. Use File.getExternalStorageDirectory() for that
instead, or some other spot.

Here is another sample project:

https://github.com/commonsguy/cw-advandroid/tree/master/Camera/Picture

This one is more complex, because it uses a front-facing camera on
Android 2.3+, but otherwise it does the same basic things as the
example you cited.






-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books 

-- 



Other Threads

1. Customizing ListAdapter

Hi,

I have a ListActivity for which I am setting up ListAdapter like below:

        phoneListAdapter = new SimpleCursorAdapter(this,
R.layout.contacts_list_row,
                        managedCursor, new String[] { PhonesColumns.NUMBER,
PhonesColumns.TYPE},
                        new int[] { R.id.text1, R.id.text2 });
        setListAdapter(phoneListAdapter);

However, the output here is not readable as PhonesColumns.TYPE is
displaying number strings like 2, 1 etc. A sample output that I get is
like below:

111-222-3333 2
222-333-4444 1

I want to make it more readable by converting them to "Mobile",
"Home", "Work" etc. A sample desired output list is like below:

111-222-3333 MOBILE
222-333-4444 WORK

Essentially, I don't want the columns to be displayed as is. How do I
achieve this ? My contacts_list_row.xml is like below:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView android:id="@+id/text2"
    android:paddingLeft="10px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

--~--~---------~--~----~------------~-------~--~----~

2. Receive notification for onDestroy, onStop, and onPause etc in other applications?

Just to clarify: by this I meant to receive notification when
onDestroy, onStop etc in other applications are called, i.e. somehow
monitor the life cycle of apps running on the phone.



--~--~---------~--~----~------------~-------~--~----~

3. How To Start a RTSP connection

4. Problem with list.setOnItemSelectedListener (new OnItemSelectedListener() {});

5. Location and context aware service in Android

6. get the images from media/internal/images/media

7. diaplay images