Android46_攝像頭

攝像頭

一、使用Intent拍攝照片:
(一)、使用步驟:
  • 1、啓動系統原生的Camera程序來拍攝照片: intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    • 最簡單的方法是使用:MediaStore.ACTION_IMAGE_CAPTURE動作觸發一個Intent。這將啓動一個Camera應用程序來拍攝照片,不需要重寫原生Camera應用程序,就可以爲用戶提供全套的攝像頭功能。
    • 默認情況下,拍攝的照片將作爲一個縮略圖返回,通過返回的Intent的extra(該extra的鍵名爲data)可以訪問原始位圖。
  • 2、指定一個用於存儲拍攝照片的目標文件,圖片路徑被編碼在Uri對象中 : intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    • 要獲得完整圖像,必須指定一個用於存儲該圖像的目標文件,該File對象將被編碼爲Uri對象,並在啓動Intent後,在putExtra()方法中作爲參數傳入。這樣攝像頭拍攝的完整圖像就被保存在了指定位置。
  • 3、照片通過onActivityResult()處理程序將收到的Intent返回給應用程序: 
    • 可以將接收到的照片先進行裁剪,然後再次保存進Uri中所指定的文件路徑;
      • intent.setAction("com.android.camera.action.CROP");
      • intent.setDataAndType(imageUri, "image/*"); 
      • intent.putExtra("scale", true);
      • intent.putExtra("output", imageUri);
    • 然後再將裁切好的圖片顯示在Activity指定ImageView中。
      • Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath()); 
      • imageView_main_show.setImageBitmap(bm);

(二)、核心代碼:

1、核心代碼:

public class MainActivity extends Activity {

private ImageView imageView_main_show;

private Uri imageUri = null;

private File file = null;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView_main_show = (ImageView) findViewById(R.id.imageView_main_show);

}


public void clickButton(View view) {

file = new File(Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/mypic.jpg");

try {

if (file.exists()) {

file.delete();

}

} catch (Exception e) {

e.printStackTrace();

}

imageUri = Uri.fromFile(file);

switch (view.getId()) {

case R.id.button_main_takephoto:

Intent intent = new Intent();

// 啓動系統原生的Camera程序來拍攝照片。

intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

// 指定一個用於存儲該圖像的目標文件,圖片路徑被編碼在Uri對象中

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);


// 或者:以下的寫法:

// intent.setAction("android.media.action.IMAGE_CAPTURE");

// intent.putExtra("output", imageUri);

startActivityForResult(intent, 1);

break;


case R.id.button_main_choose:

Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);

// Intent intent2 = new Intent("android.intent.action.GET_CONTENT");

intent2.setType("image/*");

intent2.putExtra("crop", true);

intent2.putExtra("scale", true);

intent2.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent2, 2);

break;

}

}


@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1 && resultCode == RESULT_OK) {

Intent intent = new Intent();

intent.setAction("com.android.camera.action.CROP");

intent.setDataAndType(imageUri, "image/*");

intent.putExtra("scale", true);

intent.putExtra("output", imageUri);

startActivityForResult(intent, 2);

}

if (requestCode == 2 && resultCode == RESULT_OK) {

Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());

imageView_main_show.setImageBitmap(bm);

}

}


}



二、直接控制攝像頭:【五部曲】
(一)、使用步驟:
1、爲了直接訪問攝像頭硬件,需要權限:
    <uses-permission android:name="android.permission.CAMERA"/>

2、打開攝像頭:Camera.open();
        必要時可以設置攝像頭和圖像的參數;

3、使用攝像頭預覽:(攝像頭預覽是使用SurfaceHolder實現的,必須在UI中包含一個SurfaceView。)
  • 初始化SurfaceView:
    • surfaceView_main = (SurfaceView) findViewById(R.id.surfaceView_main);
    • surfaceHolder = surfaceView_main.getHolder();
    • surfaceHolder.addCallback();
  • 在surfaceCreated()中開啓預覽:
    • mCamera.setPreviewDisplay(surfaceHolder);
    • mCamera.startPreview();
  • 在surfaceDestroyed()中停止預覽:
    • mCamera.stopPreview();

4、拍攝照片:
  • 調用Camera對象的takePicture()方法實現拍照:
    • mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
  • 獲取拍照的三個回調對象參數:
    • ShutterCallback(shutterCallback 當快門關閉時執行的操作);
    • PictureCallback(rawCallback 對圖像原始數據做處理);
    • PictureCallback(jpegCallback 對圖像的JPEG數據進行操作)。

5、釋放攝像頭:
  • 在onPause()生命週期中釋放攝像頭:
    • mCamera.release();
  • 在onResume()生命週期再打開攝像頭:
    • Camera.open();

(二)、核心代碼:       

1、核心代碼:

 public class MainActivity extends Activity {

private Camera mCamera = null;

private SurfaceView surfaceView_main;

private SurfaceHolder surfaceHolder;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


}


private void initSurfaceView() {

surfaceView_main = (SurfaceView) findViewById(R.id.surfaceView_main);

surfaceHolder = surfaceView_main.getHolder();

// surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

surfaceHolder.addCallback(new SurfaceHolder.Callback() {


@Override

public void surfaceDestroyed(SurfaceHolder holder) {

if (mCamera != null) {

mCamera.stopPreview();

}

}


@Override

public void surfaceCreated(SurfaceHolder holder) {

try {

mCamera.setPreviewDisplay(surfaceHolder);

mCamera.startPreview();

} catch (Exception e) {

e.printStackTrace();

}

}


@Override

public void surfaceChanged(SurfaceHolder holder, int format,

int width, int height) {

// TODO Auto-generated method stub


}

});

}


@Override

protected void onResume() {

super.onResume();

if (mCamera == null) {

mCamera = Camera.open();

}

}


@Override

protected void onPause() {

super.onPause();

if (mCamera != null) {

mCamera.release();

}

}


public void clickButton(View view) {

switch (view.getId()) {

case R.id.button_main_preview:

initSurfaceView();

break;


case R.id.button_main_takepicture:

mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);

break;

}

}


ShutterCallback shutterCallback = new ShutterCallback() {


@Override

public void onShutter() {

// TODO Auto-generated method stub


}

};


PictureCallback rawCallback = new PictureCallback() {


@Override

public void onPictureTaken(byte[] data, Camera camera) {

// TODO Auto-generated method stub


}

};


PictureCallback jpegCallback = new PictureCallback() {


@Override

public void onPictureTaken(byte[] data, Camera camera) {

setTitle(data.length + "");

}

};


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}




三、播放器:
(一)、使用Intent錄製視頻:

1、核心代碼:

 





(二)、MediaPlayer播放視頻:

1、核心代碼:

 




(三)、SoundPool聲音池:

1、核心代碼:

 



良心的公衆號,更多精品文章,不要忘記關注哈

《Android和Java技術棧》

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