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