Android調(diào)用外置攝像頭的方法
本文實(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í)有所幫助,也希望大家多多支持腳本之家。
- Androidstudio調(diào)用攝像頭拍照并保存照片
- Android調(diào)用手機(jī)攝像頭拍照和錄音功能
- Android實(shí)現(xiàn)調(diào)用攝像頭拍照并存儲(chǔ)照片
- Android調(diào)用系統(tǒng)攝像頭拍照并顯示在ImageView上
- Android實(shí)現(xiàn)調(diào)用攝像頭拍照與視頻功能
- Android實(shí)現(xiàn)攝像頭拍照功能
- Android調(diào)用攝像頭拍照開發(fā)教程
- Android實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能
- Android 開發(fā)隨手筆記之使用攝像頭拍照
- Android實(shí)現(xiàn)控制攝像頭拍照
相關(guān)文章
Android使用自定義PageTransformer實(shí)現(xiàn)個(gè)性的ViewPager動(dòng)畫切換效果
這篇文章主要介紹了Android使用自定義PageTransformer實(shí)現(xiàn)個(gè)性的ViewPager切換動(dòng)畫,具有很好的參考價(jià)值,一起跟隨小編過來看看吧2018-05-05
Android Dialog 動(dòng)畫實(shí)例詳解
這篇文章主要介紹了Android Dialog 動(dòng)畫實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android實(shí)現(xiàn)定時(shí)任務(wù)及鬧鐘
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)任務(wù)及鬧鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
AndroidStudio3 支持 Java8 了請(qǐng)問你敢用嗎
Google 發(fā)布了 AS 3.0,以及一系列的 Support 包,有意思的新東西挺多,AS3里面有一個(gè)亮眼的特性就是支持J8。接下來通過本文給大家分享AndroidStudio3 支持 Java8 的相關(guān)內(nèi)容,感興趣的朋友一起看看吧2017-11-11
Android實(shí)現(xiàn)多點(diǎn)觸控,自由縮放圖片的實(shí)例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)多點(diǎn)觸控,自由縮放圖片的實(shí)例代碼,可以自由地對(duì)圖片進(jìn)行縮放和移動(dòng),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
Android簡(jiǎn)單自定義音樂波動(dòng)特效圖
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單自定義音樂波動(dòng)特效圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04

