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實(shí)現(xiàn)微信朋友圈發(fā)布動(dòng)態(tài)功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)微信朋友圈發(fā)布動(dòng)態(tài)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Android ListView實(shí)現(xiàn)簡(jiǎn)單列表功能
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)簡(jiǎn)單列表功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android SDK Manager解決更新時(shí)的問題 :Failed to fetch URL...
本文主要介紹解決安裝使用SDK Manager更新時(shí)的問題:Failed to fetch URL...,這里提供了詳細(xì)的資料及解決問題辦法,有需要的小伙伴可以參考下2016-09-09Android組件創(chuàng)建DrawerLayout導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Android組件創(chuàng)建DrawerLayout導(dǎo)航的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android高手進(jìn)階教程(二十二)之Android中幾種圖像特效處理的集錦匯總!!
本篇文章主要介紹了Android中幾種圖像特效處理,比如圓角,倒影,還有就是圖片縮放,Drawable轉(zhuǎn)化為Bitmap,Bitmap轉(zhuǎn)化為Drawable等,有需要的可以了解一下。2016-11-11Android PickerView底部選擇框?qū)崿F(xiàn)流程詳解
本次主要介紹Android中底部彈出框的使用,使用兩個(gè)案例來說明,首先是時(shí)間選擇器,然后是自定義底部彈出框的選擇器,以下來一一說明他們的使用方法2022-09-09