<!-- lang: xml --> 拍照必須設置權限:
<uses-permission android:name="android.permission.CAMERA"/>java
一、佈局main.xmlandroid
<?xml version="1.0" encoding="utf-8"?>ide
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" > <SurfaceView android:id="@+id/mySurfaceView" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="200px" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/btnOpen" android:textSize="18px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打開" /> <Button android:id="@+id/btnClose" android:textSize="18px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="關閉" /> <Button android:id="@+id/btnTake" android:textSize="18px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" /> </LinearLayout>
<ImageView android:id="@+id/myImageView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>佈局
<!-- lang: java --> 二、初始化組件: private SurfaceView mySurfaceView = null;//SurfaceView的引用 private SurfaceHolder mySurfaceHolder = null;//SurfaceHolder的引用 private Button btnOpen = null;//打開按鈕 private Button btnClose = null;//關閉按鈕 private Button btnTake = null;//拍照按鈕
添加監聽器:OnClickListener, SurfaceHolder.Callback super.onCreate(savedInstanceState);this
//全屏 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); mySurfaceView = (SurfaceView) findViewById(R.id.mySurfaceView);//獲得SurfaceView的引用 btnOpen = (Button) findViewById(R.id.btnOpen);//獲得按鈕的引用 btnClose = (Button) findViewById(R.id.btnClose);//獲得按鈕的引用 btnTake = (Button) findViewById(R.id.btnTake);//獲得按鈕的引用 btnOpen.setOnClickListener(this);//爲按鈕添加監聽 btnClose.setOnClickListener(this);//爲按鈕添加監聽 btnTake.setOnClickListener(this);//爲按鈕添加監聽 mySurfaceHolder = mySurfaceView.getHolder();//得到SurfaceHolder mySurfaceHolder.addCallback(this);//添加接口的實現 mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
三、使用CAMERA拍照 private Camera myCamera = null;//Camera的引用 boolean isView = false;//是否在瀏覽中code
1)、初始化照相機:
public void initCamera(){ if(!isView){ myCamera = Camera.open(); } if(myCamera != null && !isView){ try { Camera.Parameters myParameters = myCamera.getParameters(); myParameters.setPictureFormat(PixelFormat.JPEG); //真機刪除setPreviewSize方法 myParameters.setPreviewSize(200, 200);//屏幕大小 myCamera.setParameters(myParameters); myCamera.setPreviewDisplay(mySurfaceHolder); myCamera.startPreview();//當即運行Preview } catch (IOException e) {//捕獲異常 e.printStackTrace();//打印錯誤信息 } isView = true; } }orm
二、打開/關閉和拍照 @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { //打開照相機 case R.id.btnOpen: initCamera(); break; case R.id.btnClose: if(myCamera != null && isView){//當正在顯示時 isView = false; myCamera.stopPreview(); myCamera.release(); myCamera = null; } break; //拍照 case R.id.btnTake: myCamera.takePicture (myShutterCallback, myRawCallback, myjpegCallback); break; } } //相機快門關閉 ShutterCallback myShutterCallback = new ShutterCallback() { @Override public void onShutter() { // TODO Auto-generated method stub } }; //照片二進制流生成 PictureCallback myRawCallback = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // TODO Auto-generated method stub } }; //預覽圖片 PictureCallback myjpegCallback = new PictureCallback(){ @Override public void onPictureTaken(byte[] data, Camera camera) { Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); ImageView myImageView = (ImageView) findViewById(R.id.myImageView); myImageView.setImageBitmap(bm);//將圖片顯示到下方的ImageView中 isView = false; myCamera.stopPreview(); myCamera.release(); myCamera = null; initCamera();//初始化相機 } };