关于Android接入外接摄像头,首先毋庸置疑的是需要给你的app配置相应的权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1.首先构建相应的视图view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextureView
android:id="@+id/textureview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<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_start"
android:contentDescription="@string/app_name"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
2.获取到相应的camera对象,并完成拍照进行本地存储
package com.example.onecamera;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity implements SurfaceTextureListener,
OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private Camera mCamera;
private ImageButton mPlayButton;
private boolean isRecord;
private MediaRecorder mMediaRecorder;
private CamcorderProfile mProfile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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(1);
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();
}
}
在完成以上操作之后仍无法调用到摄像头,请注意一下方法:mCamera = Camera.open(1); 这个并不是所有的android系统都是这个,有可能是mCamera = Camera.open(0); 这个根据实际情况来定
如果你做了以上操作成功了,但是移入到项目里面发现并不能行,那么请先检查你的camera是否有预览视图TextureView,如果你在布局文件里面没有放置TextureView来给camera显示,那么可能会导致你的camera无法进行拍照(我就是遇到这个坑,然后一直找不到原因,后来配置了一个宽高1dp的视图放在父布局里面就可以正常使用拍照了)。
网友评论