andorid 簡單的自定義相機

android 自定義相機

1.學習 自定義相機 所用到的類 硬件類android.hardware.Camera 。
2.預覽類 android.view.SurfaceView 預覽輔助類 android.view.SurfaceHolder。

注意事項

  1. Camera 的執行順序 流程控制 配合activity 生命週期 調控攝像機2.Camera 屬性設置 主要是 預覽大小設置 和 圖片大小設置。
    3.將持續優化…^-^。

代碼部分

1.首先是activity 類

package org.zhx.view.camera.ui;

import java.io.IOException;
import org.zhx.view.R;
import org.zhx.view.camera.util.DisplayUtil;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 
 * 希望有一天可以開源出來 org.zhx
 * 
 * @version 1.0, 2015-11-15 下午5:22:17
 * @author zhx
 */
public class ZCameraBaseAcitivy extends Activity implements PictureCallback,
        Callback, OnClickListener {
    private static final String TAG = ZCameraBaseAcitivy.class.getSimpleName();

    public static BitmapFactory.Options opt;
    static {
        // 縮小原圖片大小
        opt = new BitmapFactory.Options();
        opt.inSampleSize = 2;
    }

    private SurfaceView mPreView;
    private SurfaceHolder mHolder;

    private Camera mCamera;
    private boolean isPreview = false;
    private Point displayPx;
    private ImageView tpImg, showImg;
    private Button saveBtn;

    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zcamera_base_layout);

        displayPx = DisplayUtil.getScreenMetrics(this);
        mPreView = (SurfaceView) findViewById(R.id.z_base_camera_preview);
        tpImg = (ImageView) findViewById(R.id.z_take_pictrue_img);
        saveBtn = (Button) findViewById(R.id.z_base_camera_save);
        showImg = (ImageView) findViewById(R.id.z_base_camera_showImg);

        saveBtn.setOnClickListener(this);
        showImg.setOnClickListener(this);
        tpImg.setOnClickListener(this);
        mHolder = mPreView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        if (!isPreview) {
            mCamera = Camera.open();
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (mCamera != null) {
            if (isPreview) {
                mCamera.stopPreview();
                isPreview = false;
            }

        }
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();

        if (mCamera != null) {
            if (!isPreview) {
                mCamera.startPreview();
                // 自動對焦
                mCamera.autoFocus(null);
                isPreview = true;
            }

        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        initCamera();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        // 當holder被回收時 釋放硬件
        if (mCamera != null) {
            if (isPreview) {
                mCamera.stopPreview();
            }
            mCamera.release();
            mCamera = null;
        }


    };

    /**
     * 
     * @param value
     * @return
     * @throws Exception
     * @author zhx
     */
    public void initCamera() {

        if (mCamera != null && !isPreview) {
            try {
                Camera.Parameters parameters = mCamera.getParameters();
                // 設置閃光燈爲自動
                parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
                mCamera.setParameters(parameters);
                resetCameraSize(parameters);
                // 設置圖片格式
                parameters.setPictureFormat(ImageFormat.JPEG);
                // 設置JPG照片的質量
                parameters.set("jpeg-quality", 100);
                // 通過SurfaceView顯示取景畫面
                mCamera.setPreviewDisplay(mHolder);
                // 開始預覽
                mCamera.startPreview();
                // 自動對焦 括號內 的參數爲 對焦回掉 null 則不需要回掉 (稍後的版本會講到這個)
                mCamera.autoFocus(null);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            isPreview = true;
        }

    }

    /**
     * 旋轉相機和設置預覽大小
     * 
     * @param parameters
     */
    public void resetCameraSize(Parameters parameters) {
        if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
            //
            mCamera.setDisplayOrientation(90);
        } else {
            mCamera.setDisplayOrientation(0);
        }
        // 設置預覽圖片大小 爲設備 長寬
        parameters.setPreviewSize(displayPx.x, displayPx.y);
        // 設置圖片大小 爲設備 長寬
        parameters.setPictureSize(displayPx.x, displayPx.y);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.z_take_pictrue_img:
            // 第一個參數 是拍照的聲音,未壓縮的數據,壓縮後的數據
            mCamera.takePicture(null, null, this);
            break;

        default:
            break;
        }
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        // TODO Auto-generated method stub
        // 拍照回掉回來的 圖片數據。
        Bitmap bitmap = BitmapFactory
                .decodeByteArray(data, 0, data.length, opt);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

            Matrix matrix = new Matrix();
            matrix.setRotate(90, 0.1f, 0.1f);
            showImg.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, false));
            recycleBitmap(bitmap);
        } else {
            showImg.setImageBitmap(bitmap);
        }

        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.startPreview();
            isPreview = true;
        }
    }
/**
 * 
* @param value 
* @return 
* @throws Exception 
* @author zhx
 */
    public static void recycleBitmap(Bitmap bitmap) {
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }

}


Preview = true;
}
}
/**
*
* @param value
* @return
* @throws Exception
* @author zhx
*/
public static void recycleBitmap(Bitmap bitmap) {
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
}

}

“`
2.0 xml 配置

“` android

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.zhx.view"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="org.zhx.view.camera.ui.ZCameraBaseAcitivy" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.c
amera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

</manifest>

4.用到的id

<item name="z_base_camera_preview" type="id"/>
    <item name="z_take_pictrue_img" type="id">
    <item name="z_base_camera_save" type="id"/>
    <item name="z_base_camera_showImg" type="id"/>
    <item name="z_base_camera_show_layout" type="id"/>
    <item name="z_base_camera_show_img" type="id"/>

源碼地址:http://download.csdn.net/detail/u013783167/9270973

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章