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

Android調(diào)用外置攝像頭的方法

 更新時(shí)間:2022年03月28日 17:19:04   作者:北極熊的微笑  
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用外置攝像頭的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android調(diào)用外置攝像頭的具體代碼,供大家參考,具體內(nèi)容如下

1、布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? xmlns:android="http://schemas.android.com/apk/res/android">

? ? <TextureView
? ? ? ? android:id="@+id/textureview"
? ? ? ? android:layout_width="1dp"
? ? ? ? android:layout_height="1dp"/>

? ? <ImageButton
? ? ? ? android:id="@+id/play"
? ? ? ? android:layout_width="60dp"
? ? ? ? android:layout_height="60dp"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:background="@drawable/ic_launcher_background"
? ? ? ? android:contentDescription="@string/app_name"
? ? ? ? android:layout_marginBottom="10dp"/>

</RelativeLayout>

2、相應(yīng)的MainActivity.java的主要代碼如下

package com.deepreality.takephotowithusbcamera;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, View.OnClickListener {

? ? private static final String TAG = MainActivity.class.getSimpleName();
? ? private Camera mCamera;
? ? private ImageButton mPlayButton;

? ? private RxPermissions rxPermissions;
? ? private int permissionNum;

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);

? ? ? ? rxPermissions = new RxPermissions(MainActivity.this);
? ? ? ? checkUserAllPermissions();

? ? ? ? mPlayButton = (ImageButton) findViewById(R.id.play);
? ? ? ? mPlayButton.setOnClickListener(this);
? ? ? ? ((TextureView) findViewById(R.id.textureview))
? ? ? ? ? ? ? ? .setSurfaceTextureListener(this);
? ? }

? ? private void takePic() {
? ? ? ? if (mCamera != null) {
? ? ? ? ? ? //調(diào)用抓拍攝像頭抓拍
? ? ? ? ? ? mCamera.takePicture(null, null, pictureCallback);
? ? ? ? } else {
? ? ? ? ? ? Log.e("TAG", "請(qǐng)檢查攝像頭!");
? ? ? ? }
? ? }

? ? private Bitmap mBitmap;
? ? public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {

? ? ? ? @Override
? ? ? ? public void onPictureTaken(byte[] data, Camera camera) {
? ? ? ? ? ? Log.i("ygy", "onPictureTaken");
? ? ? ? ? ? SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式
? ? ? ? ? ? System.out.println(df.format(new Date()));// new Date()為獲取當(dāng)前系統(tǒng)時(shí)間
? ? ? ? ? ? String picName = df.format(new Date());
? ? ? ? ? ? Toast.makeText(getApplicationContext(), "正在保存...", Toast.LENGTH_LONG).show();
? ? ? ? ? ? mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
? ? ? ? ? ? File file = new File("/storage/emulated/0/" + picName + ".jpg");
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? file.createNewFile();
? ? ? ? ? ? ? ? BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
? ? ? ? ? ? ? ? mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
? ? ? ? ? ? ? ? os.flush();
? ? ? ? ? ? ? ? os.close();
? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "圖像保存成功", Toast.LENGTH_LONG).show();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }

? ? };

? ? @Override
? ? public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
? ? ? ? mCamera = Camera.open(0);
? ? ? ? if (mCamera != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? mCamera.setPreviewTexture(surface);
? ? ? ? ? ? ? ? mCamera.startPreview();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? Log.d("TAG", e.getMessage());
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? @Override
? ? protected void onStop() {
? ? ? ? if (mCamera != null) {
? ? ? ? ? ? mCamera.stopPreview();
? ? ? ? ? ? mCamera.release();
? ? ? ? ? ? mCamera = null;
? ? ? ? }
? ? ? ? super.onStop();
? ? }

? ? @Override
? ? public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

? ? }

? ? @Override
? ? public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
? ? ? ? if (mCamera != null) {
? ? ? ? ? ? mCamera.stopPreview();
? ? ? ? ? ? mCamera.release();
? ? ? ? ? ? mCamera = null;
? ? ? ? }
? ? ? ? return false;
? ? }

? ? @Override
? ? public void onSurfaceTextureUpdated(SurfaceTexture surface) {

? ? }

? ? @Override
? ? public void onClick(View v) {
? ? ? ? if (mCamera == null) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? takePic();
? ? }

? ? /**
? ? ?* 檢查并獲取用戶權(quán)限
? ? ?*/
? ? private void checkUserAllPermissions() {
? ? ? ? rxPermissions
? ? ? ? ? ? ? ? .requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE,
? ? ? ? ? ? ? ? ? ? ? ? Manifest.permission.CAMERA
? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? .subscribe(permission -> {
? ? ? ? ? ? ? ? ? ? if (permission.granted) {
? ? ? ? ? ? ? ? ? ? } else if (permission.shouldShowRequestPermissionRationale) {
? ? ? ? ? ? ? ? ? ? } else {}
? ? ? ? ? ? ? ? ? ? permissionNum ++;
? ? ? ? ? ? ? ? ? ? if (permissionNum == 2) {

? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? }
}

3、注意在清單文件里AndroidManifest.xml添加用戶權(quán)限

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

炎陵县| 伊春市| 阜城县| 方城县| 休宁县| 灌阳县| 台湾省| 图木舒克市| 凤台县| 泉州市| 宝丰县| 芜湖市| 麻城市| 太仓市| 上杭县| 平和县| 岑溪市| 江都市| 兴城市| 清镇市| 枝江市| 北安市| 延安市| 专栏| 从化市| 瑞昌市| 乐山市| 孝义市| 察雅县| 凉山| 濉溪县| 乌拉特前旗| 永德县| 神池县| 宿州市| 梅州市| 洪泽县| 抚松县| 阳曲县| 东阿县| 三明市|