Android調(diào)用手機攝像頭的方法
本文實例為大家分享了Android調(diào)用手機攝像頭的具體代碼,供大家參考,具體內(nèi)容如下
根據(jù)<第一行代碼>進行改寫:
布局文件,只有一個按鈕,和一個Imageview,imageview用于顯示拍下后的圖片
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:orientation="vertical"> ? ? ? ? <Button ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/take_photo" ? ? ? ? ? ? android:text="Take Photo"/> ? ? ? ? <ImageView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_gravity="center_horizontal" ? ? ? ? ? ? android:id="@+id/picture"/> ? ? </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.choosepictest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
? ? public static final int TAKE_PHOTO=1;
? ? private Button takePhoto;
? ? private ImageView picture;
? ? private Uri imageUri;
? ? public static File tempFile;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? takePhoto = (Button) findViewById(R.id.take_photo);
? ? ? ? picture = (ImageView) findViewById(R.id.picture);
? ? ? ? takePhoto.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? tempFile=new File(getExternalCacheDir(),"output_image.jpg");
? ? ? ? ? ? ? ? if(tempFile.exists())
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? tempFile.delete();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? tempFile.createNewFile();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? imageUri= FileProvider.getUriForFile(MainActivity.this,
? ? ? ? ? ? ? ? ? ? ? ? ? ? "com.example.choosepictest.fileprovider",tempFile);
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? Uri.fromFile(tempFile);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
? ? ? ? ? ? ? ? intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
? ? ? ? ? ? ? ? startActivityForResult(intent,TAKE_PHOTO);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? protected void onActivityResult(int requestCode,int resultCode,Intent data)
? ? {
? ? ? ? super.onActivityResult(requestCode,requestCode,data);
? ? ? ? switch (requestCode)
? ? ? ? {
? ? ? ? ? ? case TAKE_PHOTO:
? ? ? ? ? ? if(resultCode==Activity.RESULT_OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Bitmap bitmap= null;
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? bitmap = BitmapFactory.decodeStream(getContentResolver()
? ? ? ? ? ? ? ? ? ? .openInputStream(imageUri));
? ? ? ? ? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? picture.setImageBitmap(rotateIfRequired(bitmap));
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? private Bitmap rotateIfRequired(Bitmap bitmap)
? ? {
? ? ? ? String path=tempFile.getPath();
? ? ? ? ExifInterface exif = null;
? ? ? ? try {
? ? ? ? ? ? exif=new ExifInterface(path);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? int orientation= exif.getAttributeInt(ExifInterface.TAG_EXIF_VERSION,
? ? ? ? ? ? ? ? ExifInterface.ORIENTATION_NORMAL);
? ? ? ? switch (orientation)
? ? ? ? {
? ? ? ? ? ? case ExifInterface.ORIENTATION_ROTATE_90:
? ? ? ? ? ? ? ? bitmap=rotateBitmap(bitmap,90);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case ExifInterface.ORIENTATION_ROTATE_180:
? ? ? ? ? ? ? ? bitmap=rotateBitmap(bitmap,180);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case ExifInterface.ORIENTATION_ROTATE_270:
? ? ? ? ? ? ? ? bitmap=rotateBitmap(bitmap,270);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return bitmap;
? ? }
? ? private Bitmap rotateBitmap(Bitmap bitmap,int degree)
? ? {
? ? ? ? Matrix matrix=new Matrix();
? ? ? ? matrix.postRotate((float)degree);
? ? ? ? Bitmap rotateBitmap=Bitmap.createBitmap(bitmap,0,0,
? ? ? ? ? ? ? ? bitmap.getWidth(),bitmap.getHeight(),matrix,true);
? ? ? ? bitmap.recycle();
? ? ? ? return bitmap;
? ? }
}活動管理(mainfest):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ? ? package="com.example.choosepictest"> ? ? <application ? ? ? ? android:allowBackup="true" ? ? ? ? android:icon="@mipmap/ic_launcher" ? ? ? ? android:label="@string/app_name" ? ? ? ? android:roundIcon="@mipmap/ic_launcher_round" ? ? ? ? android:supportsRtl="true" ? ? ? ? android:theme="@style/Theme.ChoosePicTest"> ? ? ? ? <activity ? ? ? ? ? ? android:name=".MainActivity" ? ? ? ? ? ? android:exported="true"> ? ? ? ? ? ? <intent-filter> ? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" /> ? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" /> ? ? ? ? ? ? </intent-filter> ? ? ? ? </activity> ? ? ? ? <provider ? ? ? ? ? ? android:authorities="com.example.choosepictest.fileprovider" ? ? ? ? ? ? android:name="androidx.core.content.FileProvider" ? ? ? ? ? ? android:exported="false" ? ? ? ? ? ? android:grantUriPermissions="true"> ? ? ? ? ? ? <meta-data ? ? ? ? ? ? ? ? android:name="android.support.FILE_PROVIDER_PATHS" ? ? ? ? ? ? ? ? android:resource="@xml/file_paths"/> ? ? ? ? </provider> ? ? </application> </manifest>
用于指定路徑file_paths.xml:
<?xml version="1.0" encoding="utf-8" ?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <external-path ? ? ? ? name="my_images" ? ? ? ? path="/"/> </paths>
效果圖:

這是虛擬機,所以啥也看不到,真機就可以看到了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android CameraX打開攝像頭預覽教程
- Android?CameraX?打開攝像頭預覽功能
- Androidstudio調(diào)用攝像頭拍照并保存照片
- Android調(diào)用外置攝像頭的方法
- Android調(diào)用手機攝像頭拍照和錄音功能
- Android實現(xiàn)調(diào)用手機攝像頭錄像限制錄像時長
- android開發(fā)之調(diào)用手機的攝像頭使用MediaRecorder錄像并播放
- Android開發(fā)教程之調(diào)用攝像頭功能的方法詳解
- Android實現(xiàn)調(diào)用攝像頭進行拍照功能
- Android實現(xiàn)極簡打開攝像頭
相關(guān)文章
Android編程實現(xiàn)向SD卡寫入數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實現(xiàn)向SD卡寫入數(shù)據(jù)的方法,涉及Android針對SD卡狀態(tài)判斷,文件及權(quán)限操作等相關(guān)技巧,需要的朋友可以參考下2016-04-04
Android Activity的跳轉(zhuǎn)與傳值詳解
這篇文章主要介紹了Android Activity的跳轉(zhuǎn)與傳值詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android網(wǎng)絡(luò)判斷知識小結(jié)
本文通過兩段實例代碼分別給大家介紹Android中判斷當前網(wǎng)絡(luò)是否可用和Android 關(guān)于判斷應用是否有網(wǎng)絡(luò)的相關(guān)知識,對android網(wǎng)絡(luò)判斷相關(guān)知識感興趣的朋友一起學習吧2015-12-12
PullToRefreshListView實現(xiàn)多條目加載上拉刷新和下拉加載
這篇文章主要為大家詳細介紹了PullToRefreshListView實現(xiàn)多條目加載上拉刷新和下拉加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
Android實現(xiàn)短信驗證碼獲取自動填寫功能(詳細版)
這篇文章主要為大家詳細介紹了Android實現(xiàn)短信驗證碼獲取自動填寫功能,很實用的功能分享給大家,感興趣的小伙伴們可以參考一下2016-08-08

