編輯:Android開發實例
在Android中,使用攝像頭拍照一般有兩種方法, 一種是調用系統自帶的Camera,另一種是自己寫一個攝像的界面。
我們要添加如下權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.CAMERA"/>
1、調用系統Camera
調用系統自帶的Camera主要的步驟為:
(1)構造圖片存儲的路徑名
(2)使用Intent啟動Camera Activity
(3)將拍攝的圖片寫入到文件
(4)將圖片顯示在MainActivity中
首先,構造圖片名:
File filePath = new File(Environment.getExternalStorageDirectory(), "myCamera"); if(!filePath.exists()){ filePath.mkdirs(); } fileName = new File(filePath, System.currentTimeMillis() + ".jpg"); try{ if(!fileName.exists()){ fileName.createNewFile(); } }catch (Exception e){ e.printStackTrace(); }
然後,啟動Camera Activity:
// intent用來啟動系統自帶的Camera Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 將系統Camera的拍攝結果寫入到文件 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileName)); // 啟動intent對應的Activity,返回默認消息 startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER);
最後,將圖片顯示在MainActivity內。這時,我們通過重載onActivityResult()方法來獲取Camera返回的消息。
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode == Activity.DEFAULT_KEYS_DIALER){ // MainActivity接收Camera返回的消息,然後將已經寫入的圖片顯示在ImageView內 imageView.setImageURI(Uri.fromFile(fileName)); } }
完整代碼為:
import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.File; public class MainActivity extends Activity { private File fileName = null; private Button button; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); imageView = (ImageView)findViewById(R.id.imageView); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File filePath = new File(Environment.getExternalStorageDirectory(), "myCamera"); if(!filePath.exists()){ filePath.mkdirs(); } fileName = new File(filePath, System.currentTimeMillis() + ".jpg"); try{ if(!fileName.exists()){ fileName.createNewFile(); } }catch (Exception e){ e.printStackTrace(); } // intent用來啟動系統自帶的Camera Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 將系統Camera的拍攝結果寫入到文件 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileName)); // 啟動intent對應的Activity,返回默認消息 startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode == Activity.DEFAULT_KEYS_DIALER){ // MainActivity接收Camera返回的消息,然後將已經寫入的圖片顯示在ImageView內 imageView.setImageURI(Uri.fromFile(fileName)); } } }
2、自己寫一個攝像界面
自己寫攝像的界面,主要應用了SurfaceView來顯示攝像機的畫面。然後通過一個Button來保存當前的畫面。
同樣的,我們需要添加camera和SDCard權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.CAMERA"/>
首先,我們初始化這個SurfaceView,為這個SurfaceView添加一個對應的Callback即可:
private SurfaceView surfaceView; private SurfaceHolder.Callback callback; surfaceView = (SurfaceView)findViewById(R.id.surfaceView); callback = new SurfaceHolder.Callback(){ @Override public void surfaceCreated(SurfaceHolder holder) { startCamera(); // 用於啟動攝像頭 } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { stopCamera(); // 用於關閉攝像頭 } }; surfaceView.getHolder().addCallback(callback); // 將Callback綁定到SurfaceView
在啟動攝像頭的時候,首先打開攝像頭連接,然後將其圖像輸出到SurfaceView上,然後啟動攝像頭預覽即可在SurfaceView上顯示攝像頭的畫面,這裡的畫面和實際畫面相差有90度,所以我們需要將圖像旋轉90度之後才可以和拍攝的物體方向一致。
在關閉攝像頭時,只要停止預覽,然後釋放攝像頭資源即可。
public void startCamera(){ camera = Camera.open(); try { camera.setPreviewDisplay(surfaceView.getHolder()); camera.setDisplayOrientation(90); camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } public void stopCamera(){ camera.stopPreview(); camera.release(); camera = null; }
最後,是將拍攝到的圖片保存到SDCard,我們單擊Button來拍攝圖片,調用Camera.takePicture()方法,其原型為:
/** * Equivalent to takePicture(shutter, raw, null, jpeg). * * @see #takePicture(ShutterCallback, PictureCallback, PictureCallback, PictureCallback) */ public final void takePicture(ShutterCallback shutter, PictureCallback raw, PictureCallback jpeg) { takePicture(shutter, raw, null, jpeg); }
其中,shutter為按快門瞬間的回調,就是說按快門瞬間會調用ShutterCallback.onShutter()方法。raw是未壓縮的圖像的回調,即處理圖像原始數據的時候會調用PictureCallback.onPictureTaken()方法。jpeg為處理JPEG圖片時候的回調,即我們需要將圖像數據按照jpg格式保存的時候會調用這個方法,PictureCallback.onPIctureTaken()。這裡我們就調用了這個方法,從而將jpg圖片存儲到SDCard上。
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { camera.takePicture(null, null, new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { File filePath = new File(Environment.getExternalStorageDirectory(), "myCamera"); if(!filePath.exists()) { filePath.mkdirs(); } File fileName = new File(filePath, System.currentTimeMillis() + ".jpg"); fileName.createNewFile(); FileOutputStream fos = new FileOutputStream(fileName); fos.write(data); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }); } });
這樣,我們便實現了用SurfaceView預覽攝像頭畫面,點擊Button將當前預覽保存到SDCard中。
完整代碼如下:
import android.app.Activity; import android.hardware.Camera; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends Activity { private Camera camera; private Button button; private SurfaceView surfaceView; private SurfaceHolder.Callback callback; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); surfaceView = (SurfaceView)findViewById(R.id.surfaceView); callback = new SurfaceHolder.Callback(){ @Override public void surfaceCreated(SurfaceHolder holder) { startCamera(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { stopCamera(); } }; surfaceView.getHolder().addCallback(callback); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { camera.takePicture(null, null, new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { File filePath = new File(Environment.getExternalStorageDirectory(), "myCamera"); if(!filePath.exists()) { filePath.mkdirs(); } File fileName = new File(filePath, System.currentTimeMillis() + ".jpg"); fileName.createNewFile(); FileOutputStream fos = new FileOutputStream(fileName); fos.write(data); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }); } }); } public void startCamera(){ camera = Camera.open(); try { camera.setPreviewDisplay(surfaceView.getHolder()); camera.setDisplayOrientation(90); camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } public void stopCamera(){ camera.stopPreview(); camera.release(); camera = null; } }
以上所述是本文給大家介紹的關於Android 開發隨手筆記之使用攝像頭拍照的全部內容,希望大家喜歡。
上一篇文章介紹了Android記事本示例程序一並進行了部分剖析,本文繼續通過記
一、首先,我們來看一下效果圖,這是新浪微博的Tab滑動效果。&nbs
Android 調用自帶的錄制音頻程序 Android中有自帶的音頻錄制程序,我們可以通過指定一個Action MediaStore.Audio.Media.RE
單例模式定義: Ensure a class has only one instance, and provide a global point of acces