2012年5月18日 星期五

android -- Camera



Manifest:


<uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
    <uses-feature android:name="android.hardware.camera.flash"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>



<activity
            android:label="@string/app_name"
            android:name=".Cq_MyCameraActivity"
            android:screenOrientation="landscape" >


*********************************

Main.java:


public class Cq_MyCameraActivity extends Activity {
 

final int latchedOrientation = 90;
private Camera mCamera;

// Create surface for preview
private SurfaceHolder.Callback mSurfaceListener =
new SurfaceHolder.Callback() {

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.release();
mCamera = null;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
}catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
Camera.Parameters params = mCamera.getParameters();
params.set("jpeg-quality", 85);
mCamera.setParameters(params);
mCamera.startPreview();
}
};// end of mSurfaceListener


// Auto focus to take a photo
private Camera.AutoFocusCallback mAutoFocusListener =
new Camera.AutoFocusCallback() {

@Override
public void onAutoFocus(boolean success, final Camera camera) {
// TODO Auto-generated method stub
camera.autoFocus(null);
camera.takePicture(mShutterListener, null, mPictureListener);
new Thread() {

@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
try {
Thread.sleep(3000);
}catch (InterruptedException e) {
e.printStackTrace();
}
camera.startPreview();
}

}.start(); // end of Thread
}
};// end of mAutoFocusListener


// Listener for Shut process
private Camera.ShutterCallback mShutterListener =
new Camera.ShutterCallback() {

@Override
public void onShutter() {
// TODO Auto-generated method stub

}
};// end of mShutterListener

// Listener for Picture process
private Camera.PictureCallback mPictureListener =
new Camera.PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
FileOutputStream fos = null;
try {
fos = new FileOutputStream("/sdcard/MyCamera.jpg");
fos.write(data);
}catch (IOException e) {
e.printStackTrace();
}finally {
if (fos != null){
try {
fos.close();
}catch (IOException e) {
e.printStackTrace();
}
}

}
}
};// end of mPictureListener


// Press Camera photo button
private View.OnClickListener mButtonListener =
new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mCamera != null) {
mCamera.autoFocus(mAutoFocusListener);
}
}
};// end of mButtonListener


/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     
        final Window win = getWindow();
      //No Status Bar
        win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
      //No Title Bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
     // Camera preview
        SurfaceView surface = (SurfaceView) findViewById(R.id.surfaceView1);
        SurfaceHolder holder = surface.getHolder();
        holder.addCallback(mSurfaceListener);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
     // Camera photo button
        ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
        button.setOnClickListener(mButtonListener);
    }
}


******************************************

<?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="horizontal" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/camera" />

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>



沒有留言:

張貼留言