最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android調用手機攝像頭的方法

 更新時間:2022年03月28日 15:15:23   作者:加油吧,小杜  
這篇文章主要為大家詳細介紹了Android調用手機攝像頭的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android調用手機攝像頭的具體代碼,供大家參考,具體內容如下

根據<第一行代碼>進行改寫:

布局文件,只有一個按鈕,和一個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>

效果圖:

這是虛擬機,所以啥也看不到,真機就可以看到了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android自定義圖片輪播Banner控件使用解析

    Android自定義圖片輪播Banner控件使用解析

    這篇文章主要為大家詳細介紹了Android自定義圖片輪播Banner控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android 圖片縮放實例詳解

    Android 圖片縮放實例詳解

    本文主要介紹Android 圖片的縮放的功能,這里簡單示例代碼和實現效果圖,幫助大家學習理解,有興趣的小伙伴可以參考下
    2016-09-09
  • Android標題欄上添加多個Menu按鈕的實例

    Android標題欄上添加多個Menu按鈕的實例

    這篇文章主要介紹了Android標題欄上添加多個Menu按鈕的實例的相關資料,這里提供簡單實例說明如何添加多個menu按鈕的方法,需要的朋友可以參考下
    2017-09-09
  • Android自定義View實現時鐘效果

    Android自定義View實現時鐘效果

    這篇文章主要為大家詳細介紹了Android自定義View實現時鐘效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 如何使用Flutter實現58同城中的加載動畫詳解

    如何使用Flutter實現58同城中的加載動畫詳解

    這篇文章主要給大家介紹了關于如何使用Flutter實現58同城中加載動畫詳的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-10-10
  • Android自定義view之太極圖的實現教程

    Android自定義view之太極圖的實現教程

    這篇文章主要給大家介紹了關于Android自定義view之太極圖的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法

    Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的

    這篇文章主要介紹了 Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法的相關資料,需要的朋友可以參考下
    2016-01-01
  • Android 讀寫文件方法匯總

    Android 讀寫文件方法匯總

    以下是對Android中讀寫文件的方法進行了匯總介紹,需要的朋友可以過來參考下
    2013-07-07
  • Flutter使用JsBridge方式處理Webview與H5通信的方法

    Flutter使用JsBridge方式處理Webview與H5通信的方法

    這篇文章主要介紹了Flutter使用JsBridge方式處理Webview與H5通信的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Android TabLayout選項卡使用教程

    Android TabLayout選項卡使用教程

    這篇文章主要介紹了Android TabLayout選項卡使用,為什么會有這篇文章呢,是因為之前關于TabLayout的使用陸陸續(xù)續(xù)也寫了好幾篇了,感覺比較分散,且不成體系,寫這篇文章的目的就是希望能把各種效果的實現一次性講齊
    2023-04-04

最新評論

久治县| 宁城县| 南京市| 治多县| 潜山县| 哈密市| 彭水| 石柱| 新闻| 汤原县| 依兰县| 宁城县| 宽甸| 安仁县| 卓资县| 津市市| 闽侯县| 苗栗县| 阳信县| 伽师县| 霞浦县| 建水县| 大宁县| 荆门市| 凤冈县| 桂东县| 疏附县| 松桃| 肥东县| 呈贡县| 临城县| 青田县| 连山| 阜平县| 威海市| 奉贤区| 南和县| 东台市| 准格尔旗| 大姚县| 来凤县|