- package com.example.mycamera;
- import java.io.File;
- import java.io.FileOutputStream;
- import android.app.Activity;
- import android.content.Intent;
- import android.hardware.Camera;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.widget.Button;
- public class CameraActivity extends Activity
- {
- private static final String TAG = "MyCamera";
- final int latchedOrientation = 90;
- private Camera mCamera;
- private String picFileName;
- private boolean sdCardExist; //SD Card是否存在
- private String strTempFile = "MyCamera_"; //前置檔名
-
- // Create surface for preview
- private SurfaceHolder.Callback mSurfaceListener =
- new SurfaceHolder.Callback()
- {
- // Surface create
- public void surfaceCreated(SurfaceHolder holder)
- {
- mCamera = Camera.open();
- try
- {
- mCamera.setPreviewDisplay(holder);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- // Surface destroy
- public void surfaceDestroyed(SurfaceHolder holder)
- {
- mCamera.release();
- mCamera = null;
- }
- // Surface change
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
- {
- Camera.Parameters parameters = mCamera.getParameters();
- parameters.set("jpeg-quality", 85);
- mCamera.setParameters(parameters);
- mCamera.startPreview();
-
- }
- };
-
- // Auto focus to take a photo
- private Camera.AutoFocusCallback mAutoFocusListener =
- new Camera.AutoFocusCallback()
- {
- public void onAutoFocus(boolean success, final Camera camera)
- {
-
- Log.i(TAG,"AutoFocus : " + success);
-
- camera.autoFocus(null);
- camera.takePicture(mShutterListener, null, mPictureListener);
- new Thread()
- {
- @Override
- public void run()
- {
- try
- {
- Thread.sleep(3000);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- camera.startPreview();
- Intent intent = new Intent ();
- intent.putExtra ("PIC_FILENAME", picFileName);
- setResult (-1, intent);
- finish ();
- }
- }.start();
-
- }
- };
- // Listener for Shut process
- private Camera.ShutterCallback mShutterListener =
- new Camera.ShutterCallback() {
- public void onShutter() {
- Log.i(TAG, "onShutter");
- }
- };
- // Listener for Picture process
- private Camera.PictureCallback mPictureListener =
- new Camera.PictureCallback() {
- public void onPictureTaken(byte[] data, Camera camera) {
- Log.i(TAG, "Picture taken");
-
- FileOutputStream fos = null;
- try
- {
- /* 判斷SD Card是否插入 */
- sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
- /* 取得SD Card路徑做為錄音的檔案位置 */
- if (sdCardExist)
- {
- File myDir = Environment.getExternalStorageDirectory();
- File myFile = File.createTempFile(strTempFile, ".jpg",myDir);
- picFileName = myFile.getName();
- String myFileName = myFile.getPath();
- fos = new FileOutputStream(myFileName);
- fos.write(data);
- fos.close();
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
- };
- // Press Camera photo button
- private View.OnClickListener mButtonListener = new View.OnClickListener()
- {
- public void onClick(View v)
- {
- if(mCamera != null)
- {
- mCamera.autoFocus(mAutoFocusListener);
- }
- }
- };
- // Called when the activity is first created.
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.activity_camera);
- // Camera preview
- SurfaceView surface = (SurfaceView)findViewById(R.id.SurfaceView01);
- SurfaceHolder holder = surface.getHolder();
- holder.addCallback(mSurfaceListener);
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- // Camera photo button
- Button button = (Button)findViewById(R.id.ImageButton01);
- button.setOnClickListener(mButtonListener);
- }
- }
複製代碼 |