日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android調用手機攝像頭的方法_Android

作者:加油吧,小杜 ? 更新時間: 2022-05-31 編程語言

本文實例為大家分享了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>

效果圖:

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

原文鏈接:https://blog.csdn.net/weixin_43676892/article/details/123641719

欄目分類
最近更新