Android开发学习之路--Camera之初体验
? ? 顧名思義Camera就是拍照和錄像的功能,像微信里面,我們想拍照傳一下照片,就能夠通過camera來拍照,然后存儲照片。發送給好友。那么微信的app里面是不會直接通過camera api來實現的,由于系統一般都會有camera這個程序,那么直接調用camera app來實現拍照的功能不是非常方便嘛。這里我們學習下。事實上終于camera調用到android底層的是v4l2的接口,關于v4l2,還有android的camera的框架以后有機會再好好研究研究。
? ? 調用系統自帶的camera須要用到intent,通過MediaStore獲取照片路徑。以下來試一下。新建projectCameraPictureTest。為layout加入代碼例如以下:
<?xml version="1.0" encoding="utf-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="10dp"> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照"/> </LinearLayout>
? ? 編寫代碼例如以下:
package com.example.jared.camerapicturetest;import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView;import java.io.File; import java.io.FileNotFoundException; import java.io.IOException;public class MainActivity extends AppCompatActivity {public static final int TAKE_PHOTO = 1;public static final int CROP_PICTURE = 2;private Button takePhoto;private ImageView picture;private Uri imageUri;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);takePhoto = (Button)findViewById(R.id.take_photo);takePhoto.setOnClickListener(new myOnClickListener());picture = (ImageView)findViewById(R.id.picture);picture.setOnClickListener(new myOnClickListener());}private class myOnClickListener implements View.OnClickListener {@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.take_photo:setTakePhoto();break;default:break;}}}public void setTakePhoto() {File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");try {if(outputImage.exists()) {outputImage.delete();}outputImage.createNewFile();} catch (IOException e) {e.printStackTrace();}imageUri = Uri.fromFile(outputImage);Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent, TAKE_PHOTO);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch (requestCode) {case TAKE_PHOTO:if(resultCode == RESULT_OK) {Intent intent1 = new Intent("com.android.camera.action.CROP");intent1.setDataAndType(imageUri, "image/*");intent1.putExtra("scale", true);intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent1, CROP_PICTURE);}break;case CROP_PICTURE:if(resultCode == RESULT_OK) {try {Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));picture.setImageBitmap(bitmap);} catch (FileNotFoundException e) {e.printStackTrace();}}break;default:break;}} }
? ? 這里首先確定了保存的路徑為根文件夾下的test.jpg,然后通過intent,傳入這個路徑的Uri,打開相機進行拍照,這里有對拍照的返回,假設返回成功,那么就調用CROP的功能對比片進行裁剪,進入到裁減后返回成功就把圖片顯示在layout創建的ImageView中。
? ? 詳細須要真機顯示,這里再插播一段關于真機屏幕在mac電腦上的顯示,詳細能夠參考這篇文章。將你的安卓手機屏幕共享到PC或Mac上。通過一個chrome的Vysor插件來實現,須要android的5.0以上的版本號才干夠。
? ? 好了,以下看下顯示的效果:
??? ?
? ? 效果基本上出來了。非常不錯的插件。
微信里面非常多不是直接拍照發送的,還有通過選擇相冊的圖片,已經拍好的照片來發送圖片的,那么接著我們來實現這個功能。首先layout加入了choosephoto:
<?
xml version="1.0" encoding="utf-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="10dp"> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照"/> <Button android:id="@+id/choose_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選取照片"/> </LinearLayout>
? ? 接著改動MainActivity代碼例如以下:
package com.example.jared.camerapicturetest;import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView;import java.io.File; import java.io.FileNotFoundException; import java.io.IOException;public class MainActivity extends AppCompatActivity {public static final int TAKE_PHOTO = 1;public static final int CROP_PICTURE = 2;private Button takePhoto;private Button choosePhoto;private ImageView picture;private Uri imageUri;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);takePhoto = (Button)findViewById(R.id.take_photo);takePhoto.setOnClickListener(new myOnClickListener());choosePhoto = (Button)findViewById(R.id.choose_photo);choosePhoto.setOnClickListener(new myOnClickListener());picture = (ImageView)findViewById(R.id.picture);picture.setOnClickListener(new myOnClickListener());}private class myOnClickListener implements View.OnClickListener {@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.take_photo:setTakePhoto();break;case R.id.choose_photo:setChoosePhoto();default:break;}}}public void setChoosePhoto() {File outputImage1 = new File(Environment.getExternalStorageDirectory(), "test1.jpg");try {if(outputImage1.exists()) {outputImage1.delete();}outputImage1.createNewFile();} catch (IOException e) {e.printStackTrace();}imageUri = Uri.fromFile(outputImage1);Intent intent1 = new Intent("android.intent.action.GET_CONTENT");intent1.setType("image/*");intent1.putExtra("crop", true);intent1.putExtra("scale", true);intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent1, CROP_PICTURE);}public void setTakePhoto() {File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");try {if(outputImage.exists()) {outputImage.delete();}outputImage.createNewFile();} catch (IOException e) {e.printStackTrace();}imageUri = Uri.fromFile(outputImage);Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent, TAKE_PHOTO);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch (requestCode) {case TAKE_PHOTO:if(resultCode == RESULT_OK) {Intent intent1 = new Intent("com.android.camera.action.CROP");intent1.setDataAndType(imageUri, "image/*");intent1.putExtra("scale", true);intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent1, CROP_PICTURE);}break;case CROP_PICTURE:if(resultCode == RESULT_OK) {try {Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));picture.setImageBitmap(bitmap);} catch (FileNotFoundException e) {e.printStackTrace();}}break;default:break;}} }
? ? 基本上和拍照也幾乎相同。然后我們執行下看看效果:
????
? ? ?點擊選擇照片button。我們會進入到相冊的app里面,然后選擇一張照片,然后裁剪后保存。如上圖所看到的。
附:參考《第一行代碼》
轉載于:https://www.cnblogs.com/llguanli/p/7085055.html
總結
以上是生活随笔為你收集整理的Android开发学习之路--Camera之初体验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下Makefile学习笔记
- 下一篇: IP协议解读(三)