android系統的照相功能,已實現2種方法,可供大家參考:
1.調用系統網路攝影機來拍照
首先,找到AndroidManifest.xml檔案裡加入使用者權限
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
其次,在主類java檔案裡加入2個控制項(button和imageview),是用來觸發按鈕事件和顯示圖片的,純是個人愛好
final int TAKE_PICTURE = 1;//為了表示返回方法中辨識你的程式開啟的相機
關鍵是這裡:startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), TAKE_PICTURE);
是開啟系統內建相機,以下是處理拍照得到的資料,將資料儲存下來
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
if (resultCode == RESULT_OK) {
Bitmap bm = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(bm);//想映像顯示在ImageView視圖上,private ImageView img;
File myCaptureFile = new File("sdcard/123456.jpg");
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
/* 採用壓縮轉檔方法 */
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
/* 調用flush()方法,更新BufferStream */
bos.flush();
/* 結束OutputStream */
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
這樣就能實現調用系統內建的網路攝影機了,很簡單的操作。
2.自己寫程式來儲存照片
照片格局檔案lay.xml裡要先進行這些定義
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="130px"
android:paddingRight="200px"
>
<SurfaceView
android:id="@+id/mSurfaceView1"
android:visibility="visible"
android:layout_width="320px"
android:layout_height="240px">
</SurfaceView>
</LinearLayout>
</LinearLayout>
其中SurfaceView是用來進行預覽的,
在Oncreat函數裡初始化一系列的值:
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.lay);
/* 取得螢幕解析像素 */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// mImageView01 = (ImageView) findViewById(R.id.myImageView1);
/* 以SurfaceView作為相機Preview之用 */
mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1);
/* 綁定SurfaceView,取得SurfaceHolder對象 */
mSurfaceHolder01 = mSurfaceView01.getHolder();
/* Activity必須實現SurfaceHolder.Callback */
mSurfaceHolder01.addCallback(takephoto.this);
/*
* 以SURFACE_TYPE_PUSH_BUFFERS(3)
* 作為SurfaceHolder顯示類型
* */
mSurfaceHolder01.setType
(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
首先進行初始化照相機的功能函數和參數設定:
private Camera mCamera01;
mCamera01 = Camera.open();
/* 建立Camera.Parameters對象 */
Camera.Parameters parameters = mCamera01.getParameters();
/* 設定相片格式為JPEG */
parameters.setPictureFormat(PixelFormat.JPEG);
Log.i(TAG, "pic is jpeg");
/* 指定preview的螢幕大小 */
parameters.setPreviewSize(320, 240);
Log.i(TAG, "pic pingmu fenbianlv");
/* 設定圖片解析度大小 */
parameters.setPictureSize(1024, 768);
Log.i(TAG, "pic tupian fenbianlv");
/* 將Camera.Parameters設定予Camera */
mCamera01.setParameters(parameters);
/* setPreviewDisplay唯一的參數為SurfaceHolder */
mCamera01.setPreviewDisplay(mSurfaceHolder01);
/* 立即運行Preview */
mCamera01.startPreview();
初始化成功後就可以進行拍照了,拍照函數依然是通過調用camera類的函數來實現
mCamera01.takePicture
(shutterCallback, rawCallback, jpegCallback);
只需實現jpegCallback這個回呼函數來就行解碼、儲存即可,前2個參數可以直接設為null,不過系統一般會自動幫你把這些都寫進來的
private PictureCallback jpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] _data, Camera _camera)
{
// TODO Handle JPEG image data
/* onPictureTaken傳入的第一個參數即為相片的byte */
Bitmap bm = BitmapFactory.decodeByteArray
(_data, 0, _data.length);
/* 建立新檔案 */
picname = "sdcard/1234566.jpg";//要儲存在哪裡,路徑你自己設
File myCaptureFile = new File(picname);
try
{
BufferedOutputStream bos = new BufferedOutputStream
(new FileOutputStream(myCaptureFile));
/* 採用壓縮轉檔方法 */
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
/* 調用flush()方法,更新BufferStream */
bos.flush();
/* 結束OutputStream */
bos.close();
/* 將拍照下來且儲存完畢的圖檔案,顯示出來 */
//mImageView01.setImageBitmap(bm);
/* 顯示完圖檔案,立即重設相機,並關閉預覽 */
resetCamera();
}
catch (Exception e)
{
Log.e(TAG, e.getMessage());
}
}
};
拍照完了要重設照相機,然後可以繼續拍照
/* 相機重設 */
private void resetCamera()
{
if (mCamera01 != null && bIfPreview)
{
mCamera01.stopPreview();
/* 擴充學習,釋放Camera對象 */
mCamera01.release();
mCamera01 = null;
bIfPreview = false;
}
}
2種拍照方式的比較
1.調用系統內建的照相機,照片格式大小隻有幾種選擇,照片拍出來比較大,而自己程式實現的話可以調節照片大小為任意尺寸,圖片的容量可以調節
2.調用系統的簡單,而且外觀一般比自己設定的要好看
3.調用系統的操作簡單、方便,不易出錯,自己編程的話需要注意,容易引起系統出錯意外終止