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" />