Sunday, September 23, 2012

OpenCV Android Camera in taking frames

src/CameraApplicationActivity.java

import android.app.Activity; import android.os.Bundle; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.hardware.Camera.ShutterCallback; import android.provider.MediaStore.Images; import android.util.Log; import android.view.LayoutInflater; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.Toast; public class CameraApplicationActivity extends Activity implements SurfaceHolder.Callback {     private static final String TAG = "cookbook.hardware";     private LayoutInflater mInflater = null;     Camera mCamera;     byte[] tempdata;     boolean mPreviewRunning = false;     private SurfaceHolder mSurfaceHolder;     private SurfaceView mSurfaceView;     Button takepicture;     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         getWindow().setFormat(PixelFormat.TRANSLUCENT);         requestWindowFeature(Window.FEATURE_NO_TITLE);         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,         WindowManager.LayoutParams.FLAG_FULLSCREEN);         setContentView(R.layout.main);         mSurfaceView = (SurfaceView)findViewById(R.id.surface);         mSurfaceHolder = mSurfaceView.getHolder();         mSurfaceHolder.addCallback(this);         mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);         mInflater = LayoutInflater.from(this);         View overView = mInflater.inflate(R.layout.cameraoverlay, null);         this.addContentView(overView,             new LayoutParams(LayoutParams.FILL_PARENT,             LayoutParams.FILL_PARENT));         takepicture = (Button) findViewById(R.id.button);         takepicture.setOnClickListener(new OnClickListener(){             public void onClick(View view){                 mCamera.takePicture(mShutterCallback,                 mPictureCallback, mjpeg);             }         });          }     ShutterCallback mShutterCallback = new ShutterCallback(){     @Override     public void onShutter() {}     };     PictureCallback mPictureCallback = new PictureCallback() {         public void onPictureTaken(byte[] data, Camera c) {}     };     PictureCallback mjpeg = new PictureCallback() {         public void onPictureTaken(byte[] data, Camera c) {             if(data !=null) {                 tempdata=data;                 done();             }         }     };     void done() {         Bitmap bm = BitmapFactory.decodeByteArray(tempdata,             0, tempdata.length);         String url = Images.Media.insertImage(getContentResolver(),             bm, null, null);         bm.recycle();         Bundle bundle = new Bundle();         if(url!=null) {             bundle.putString("url", url);             Intent mIntent = new Intent();             mIntent.putExtras(bundle);             setResult(RESULT_OK, mIntent);         } else {             Toast.makeText(this, "Picture can not be saved",             Toast.LENGTH_SHORT).show();         }         finish();     }     @Override     public void surfaceChanged(SurfaceHolder holder, int format, int w,         int h) {         Log.e(TAG, "surfaceChanged");         try {             if (mPreviewRunning) {                 mCamera.stopPreview();                 mPreviewRunning = false;             }             Camera.Parameters p = mCamera.getParameters();             p.setPreviewSize(w, h);             mCamera.setParameters(p);             mCamera.setPreviewDisplay(holder);             mCamera.startPreview();             mPreviewRunning = true;         } catch(Exception e) {             Log.d("",e.toString());         }     }     @Override     public void surfaceCreated(SurfaceHolder holder) {         Log.e(TAG, "surfaceCreated");         mCamera = Camera.open();     }     @Override     public void surfaceDestroyed(SurfaceHolder holder) {         Log.e(TAG, "surfaceDestroyed");         mCamera.stopPreview();         mPreviewRunning = false;         mCamera.release();         mCamera=null;     } } 

main.xml

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

cameraoverlay.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:gravity="bottom"     android:layout_gravity="bottom">     <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="horizontal"     android:gravity="center_horizontal">     <Button             android:id="@+id/button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/button_text"         />     </LinearLayout> </LinearLayout> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.duckie.dtcaa"     android:versionCode="1"     android:versionName="1.0" >     <uses-sdk android:minSdkVersion="8"         android:targetSdkVersion="15" />     <uses-permission android:name="android.permission.CAMERA" />     <application         android:icon="@drawable/dtcaa"         android:label="@string/app_name" >         <activity             android:name=".CameraApplicationActivity"             android:label="@string/app_name"             android:screenOrientation="landscape">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application> </manifest> 

this code is from the "The Android Developer's Cookbook." from the code above, where can i take frames for OpenCV image processing? i'd like to make it output a Canny Edge video stream. a sample code would be of great help.

Source: http://stackoverflow.com/questions/12552530/opencv-android-camera-in-taking-frames

franchise tag lesotho a wrinkle in time benjamin netanyahu storm shelters nick lachey chevy volt

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.