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

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)文章

  • android實(shí)現(xiàn)微信朋友圈發(fā)布動(dòng)態(tài)功能

    android實(shí)現(xiàn)微信朋友圈發(fā)布動(dòng)態(tài)功能

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)微信朋友圈發(fā)布動(dòng)態(tài)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Android View 事件分發(fā)機(jī)制詳解

    Android View 事件分發(fā)機(jī)制詳解

    本文主要介紹Android View 事件分發(fā)機(jī)制,這里整理了相關(guān)資料并詳細(xì)介紹了view分發(fā)機(jī)制的知識(shí)及簡(jiǎn)單示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • 深入Android線程的相關(guān)問題解惑

    深入Android線程的相關(guān)問題解惑

    本篇文章是對(duì)Android線程的相關(guān)問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Android中圖片占用內(nèi)存的深入分析

    Android中圖片占用內(nèi)存的深入分析

    我們?cè)谌粘i_發(fā)中應(yīng)該經(jīng)常思考這些問題,圖片占用內(nèi)存跟哪些東西有關(guān)?跟手機(jī)有關(guān)系么?這篇文章主要給大家介紹了關(guān)于Android中圖片占用內(nèi)存的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Android ListView實(shí)現(xiàn)簡(jiǎn)單列表功能

    Android ListView實(shí)現(xiàn)簡(jiǎn)單列表功能

    這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)簡(jiǎn)單列表功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android SDK Manager解決更新時(shí)的問題 :Failed to fetch URL...

    Android SDK Manager解決更新時(shí)的問題 :Failed to fetch URL...

    本文主要介紹解決安裝使用SDK Manager更新時(shí)的問題:Failed to fetch URL...,這里提供了詳細(xì)的資料及解決問題辦法,有需要的小伙伴可以參考下
    2016-09-09
  • 深入理解Android 5.0中的Toolbar

    深入理解Android 5.0中的Toolbar

    相信大家都有所體會(huì),搜索Toolbar相關(guān)文章滿天飛,但是大都不是很全面,每次要用到的時(shí)候又要重頭過濾一遍。而且隨著版本升級(jí)很多較早的文章的方法已經(jīng)失效,最近剛好好用到Toolbar,就將相關(guān)配置整理下,方便以后需要的時(shí)候或者有需要的朋友們參考學(xué)習(xí)。
    2017-01-01
  • Android組件創(chuàng)建DrawerLayout導(dǎo)航

    Android組件創(chuàng)建DrawerLayout導(dǎo)航

    這篇文章主要為大家詳細(xì)介紹了Android組件創(chuàng)建DrawerLayout導(dǎo)航的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android高手進(jìn)階教程(二十二)之Android中幾種圖像特效處理的集錦匯總!!

    Android高手進(jìn)階教程(二十二)之Android中幾種圖像特效處理的集錦匯總!!

    本篇文章主要介紹了Android中幾種圖像特效處理,比如圓角,倒影,還有就是圖片縮放,Drawable轉(zhuǎn)化為Bitmap,Bitmap轉(zhuǎn)化為Drawable等,有需要的可以了解一下。
    2016-11-11
  • Android PickerView底部選擇框?qū)崿F(xiàn)流程詳解

    Android PickerView底部選擇框?qū)崿F(xiàn)流程詳解

    本次主要介紹Android中底部彈出框的使用,使用兩個(gè)案例來說明,首先是時(shí)間選擇器,然后是自定義底部彈出框的選擇器,以下來一一說明他們的使用方法
    2022-09-09

最新評(píng)論