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

Android調(diào)用手機(jī)攝像頭的方法

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

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論