欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android打開(kāi)系統(tǒng)相機(jī)并拍照的2種顯示方法

 更新時(shí)間:2019年05月27日 09:33:25   作者:Vivinia_Vivinia  
這篇文章主要介紹了Android打開(kāi)系統(tǒng)相機(jī)并拍照的2種顯示方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android打開(kāi)系統(tǒng)相機(jī)并拍照的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下

目標(biāo)效果:

第二張為點(diǎn)擊第一個(gè)按鈕拍照后顯示的,比較模糊,第三章為點(diǎn)擊第二個(gè)按鈕拍照后顯示的,比較清楚。

1.activity_main.xml頁(yè)面設(shè)置布局。

activity_main.xml頁(yè)面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity" >
 
 <Button
  android:id="@+id/btnOpenCamera"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:text="拍照立即顯示" />
 <Button
  android:id="@+id/btnSavePhoto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="60dp"
  android:text="拍照存儲(chǔ)后顯示" />
 
 <ImageView
  android:id="@+id/ivShowPicture"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="130dp" />
</RelativeLayout>

2.MainActivity.java頁(yè)面打開(kāi)相機(jī)并獲取傳遞回來(lái)的數(shù)據(jù),兩種方式第一個(gè)比較模糊,適合小圖,第二個(gè)清楚些。

MainActivity.java頁(yè)面:

package com.example.camera;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
 
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
 
public class MainActivity extends Activity implements OnClickListener {
 
 private Button btnOpenCamera, btnSavePhoto;
 private ImageView ivShowPicture;
 private static int REQUEST_CAMERA_1 = 1;
 private static int REQUEST_CAMERA_2 = 2;
 private String mFilePath;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 // 初始化控件
 init();
 
 // 控件綁定點(diǎn)擊事件
 bindClick();
 }
 
 // 初始化控件和變量
 private void init() {
 btnOpenCamera = (Button) findViewById(R.id.btnOpenCamera);
 btnSavePhoto = (Button) findViewById(R.id.btnSavePhoto);
 ivShowPicture = (ImageView) findViewById(R.id.ivShowPicture);
 mFilePath = Environment.getExternalStorageDirectory().getPath();// 獲取SD卡路徑
 mFilePath = mFilePath + "/" + "temp.png";// 指定路徑
 }
 
 // 控件綁定點(diǎn)擊事件
 private void bindClick() {
 btnOpenCamera.setOnClickListener(this);
 btnSavePhoto.setOnClickListener(this);
 }
 
 @Override
 public void onClick(View view) {
 switch (view.getId()) {
 case R.id.btnOpenCamera:
 // 拍照并顯示圖片
 openCamera_1();
 break;
 case R.id.btnSavePhoto:
 // 拍照后存儲(chǔ)并顯示圖片
 openCamera_2();
 break;
 default:
 break;
 }
 }
 
 // 拍照并顯示圖片
 private void openCamera_1() {
 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 啟動(dòng)系統(tǒng)相機(jī)
 startActivityForResult(intent, REQUEST_CAMERA_1);
 }
 
 // 拍照后存儲(chǔ)并顯示圖片
 private void openCamera_2() {
 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 啟動(dòng)系統(tǒng)相機(jī)
 Uri photoUri = Uri.fromFile(new File(mFilePath)); // 傳遞路徑
 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系統(tǒng)默認(rèn)存儲(chǔ)路徑
 startActivityForResult(intent, REQUEST_CAMERA_2);
 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == RESULT_OK) { // 如果返回?cái)?shù)據(jù)
 if (requestCode == REQUEST_CAMERA_1) { // 判斷請(qǐng)求碼是否為REQUEST_CAMERA,如果是代表是這個(gè)頁(yè)面?zhèn)鬟^(guò)去的,需要進(jìn)行獲取
 Bundle bundle = data.getExtras(); // 從data中取出傳遞回來(lái)縮略圖的信息,圖片質(zhì)量差,適合傳遞小圖片
 Bitmap bitmap = (Bitmap) bundle.get("data"); // 將data中的信息流解析為Bitmap類(lèi)型
 ivShowPicture.setImageBitmap(bitmap);// 顯示圖片
 } else if (requestCode == REQUEST_CAMERA_2) {
 FileInputStream fis = null;
 try {
  fis = new FileInputStream(mFilePath); // 根據(jù)路徑獲取數(shù)據(jù)
  Bitmap bitmap = BitmapFactory.decodeStream(fis);
  ivShowPicture.setImageBitmap(bitmap);// 顯示圖片
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } finally {
  try {
  fis.close();// 關(guān)閉流
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 }
 }
 }
}

3.因?yàn)榇蜷_(kāi)的是系統(tǒng)相機(jī),所以不需要添加打開(kāi)相機(jī)的權(quán)限,如果想要在別的應(yīng)用里選擇打開(kāi)系統(tǒng)相機(jī)時(shí)也出現(xiàn)你的應(yīng)用,需要在AndroidManifest.xml頁(yè)面進(jìn)行設(shè)置。

AndroidManifest.xml頁(yè)面:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.camera"
 android:versionCode="1"
 android:versionName="1.0" >
 
 <uses-sdk
  android:minSdkVersion="17"
  android:targetSdkVersion="19" />
 
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
 <application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
  <activity
   android:name="com.example.camera.MainActivity"
   android:label="@string/app_name" >
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
 
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
   <!-- 注冊(cè)相機(jī)功能,在別的程序Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);進(jìn)行啟動(dòng)相機(jī)時(shí)也會(huì)選擇是否啟動(dòng)該應(yīng)用 -->
   <intent-filter>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
 
    <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
  </activity>
 </application>
 
</manifest>

4.運(yùn)行就可以顯示目標(biāo)效果了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論