您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android如何調用外置攝像頭的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Android如何調用外置攝像頭文章都會有所收獲,下面我們一起來看看吧。
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、相應的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) { //調用抓拍攝像頭抓拍 mCamera.takePicture(null, null, pictureCallback); } else { Log.e("TAG", "請檢查攝像頭!"); } } 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");//設置日期格式 System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間 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(); } /** * 檢查并獲取用戶權限 */ 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添加用戶權限
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
關于“Android如何調用外置攝像頭”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Android如何調用外置攝像頭”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。