如何打开摄像头
这篇博客主要介绍怎么利用camera来写一个自己的Activity来进行预览界面。里面会用到一个类Cameraconfiguration,里面是camera的基本属性,如果有需要的朋友可以去我另外一篇博客看看:CameraConfiguration介绍
(1)设置预览的callback:
建议自己写一个单独的类来继承系统的callback,因为你可以以后再这个预览回调里面拿到你预览的数据,进行其他的操作:比如截屏、作为扫描二维码的数据源。
预览的callback如下:
import Android.hardware.Camera;
public class PreviewCallback implements Camera.PreviewCallback {
private final CameraConfigurationManager configurationManager;
private final boolean useOneShotPreviewCallback;
@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
if (!useOneShotPreviewCallback) {
camera.setPreviewCallback(null);
}
}
PreviewCallback(CameraConfigurationManager configurationManager, boolean useOneShotPreviewCallback) {
this.configurationManager = configurationManager;
this.useOneShotPreviewCallback = useOneShotPreviewCallback;
}
}
useOneShotPreviewCallback代表是否可以启用预览回调,只有在系统版本号大于3的时候才可以启用预览回调(预览回调是在预览的时候自己执行其他自己想做的操作,是否支持预览功能)。
(2)定焦的callback:
也建议自己写一个类来继承这个回调,因为你可以在这个回调里面写方法,什么时候来执行定焦,多久执行一次。如下:
import android.hardware.Camera;
import android.os.handler;
import android.widget.toast;
import shen.da.ye.myscandemo.APPlication;
/**
* Created by cy on 2017/9/27 0027 17:46
*/
public class AutoFocusCallback implements Camera.AutoFocusCallback {
private static final String TAG = "cy===AutoFocusCallback";
private final Handler handler;
@Override
public void onAutoFocus(boolean b, final Camera camera) {
//b 代表执行定焦操作的返回值,定焦成功了返回true,执行定焦失败了返回false
if (!b) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
camera.autoFocus(AutoFocusCallback.this);
toast.maketext(Application.APPLICATION_context, "定焦一次", Toast.LENGTH_SHORT).show();
}
}, 1500);
} else {
handler.postDelayed(new Runnable() {
@Override
public void run() {
camera.autoFocus(AutoFocusCallback.this);
Toast.makeText(Application.APPLICATION_CONTEXT, "定焦一次", Toast.LENGTH_SHORT).show();
}
}, 10000);
}
}
AutoFocusCallback() {
handler = new Handler();
}
}
(3)设置CameraManage来统筹管理预览的设置以及回调:
import android.content.Context;
import android.hardware.Camera;
import android.os.build;
import android.os.Handler;
import android.util.Log;
import android.view.surfaceholder;
import java.io.IOException;
public class CameraManager {
private static final String TAG = "Cy===CameraManger";
private Context mContext = null;
static final int SDK_INT;
static {
int sdkInt;
try {
sdkInt = integer.parseInt(Build.VERSION.SDK);
} catch (numberformatexception nfe) {
// Just to be safe
sdkInt = 10000;
}
SDK_INT = sdkInt;
}
private Camera camera;
private boolean initialized;
private CameraConfigurationManager cameraConfigurationManager;
private boolean previewing = false;
private boolean usrOneShotPreviewCallback;
private AutoFocusCallback autoFocusCallback;
private PreviewCallback previewCallback;
private CameraManager() {
}
private void init() {
cameraConfigurationManager = CameraConfigurationManager
.getinstance()
.setContext(mContext);
usrOneShotPreviewCallback = Integer.parseInt(Build.VERSION.SDK) > 3;
previewCallback = new PreviewCallback(cameraConfigurationManager, usrOneShotPreviewCallback);
autoFocusCallback = new AutoFocusCallback();
}
//frame是帧的意思
public void requestPreviewFrame(Handler handler, int message) {
if (camera != null && previewing) {
previewCallback.setHandler(handler, message);
if (usrOneShotPreviewCallback) {
camera.setOneShotPreviewCallback(previewCallback);
} else {
camera.setPreviewCallback(previewCallback);
}
}
}
private static final class Holder {
private static final CameraManager CAMERA_MANAGER = new CameraManager();
}
public static CameraManager getInstance() {
return Holder.CAMERA_MANAGER;
}
public CameraManager initContext(Context context) {
mContext = context;
init();
return this;
}
/*打开相机硬件驱动程序*/
public void openDriver(SurfaceHolder holder) {
if (camera == null) {
//之所以判断非空,是因为有可能界面onpause然后onResume的时候没必要重复赋值
camera = Camera.open();
if (camera == null) {
try {
throw new IOException("打开摄像头失败");
} catch (IOException e) {
e.printstacktrace();
}
}
try {
camera.setPreviewdisplay(holder);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "设置previewDisplay失败");
}
if (!initialized) {
initialized = true;
cameraConfigurationManager.getCameraparams(camera);
}
cameraConfigurationManager.setDseiredCameraparameters(camera);
FlashlightManager.enableFlashlight();//默认让闪光灯关闭
}
}
/*关闭相机硬件驱动*/
public void closeDriver() {
if (camera != null) {
FlashlightManager.disableFlashlight();//关闭闪光灯
camera.release();
camera = null;
}
}
/*开始设置预览界面到手机屏幕上*/
public void startPreview() {
if (camera != null && !previewing) {
camera.startPreview();
previewing = true;
}
}
public void stopPreview() {
if (camera != null && previewing) {
if (!usrOneShotPreviewCallback) {
camera.setPreviewCallback(null);
}
camera.startPreview();
previewing = false;
}
}
public void requestAutoFocus(Handler handler, int message) {
if (camera != null && previewing) {
autoFocusCallback.setHandler(handler, message);
camera.autoFocus(autoFocusCallback);//找一下在那个地方setNull
}
}
}
(4)在activity中的调用:
import android.app.Activity;
import android.content.intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.surfaceview;
import shen.da.ye.myscandemo.camera.CameraManager;
/**
* Created by cy on 2017/9/27 0027 16:15
*/
public class ScanActivity extends Activity {
private static final String TAG = "cy==ScanActivity";
private SurfaceView mSurfaceView;
private boolean isHasSurface;//判断是否初始化过surfaceView
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
mSurfaceView = findViewById(R.id.scan_surface_view);
CameraManager.getInstance().initContext(this);
}
@Override
protected void onResume() {
super.onResume();
SurfaceHolder holder = mSurfaceView.getHolder();
if (isHasSurface) {
initCamera(holder);
} else {
holder.addCallback(mSurfaceCallback);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}
@Override
protected void onPause() {
super.onPause();
CameraManager.getInstance().stopPreview();
CameraManager.getInstance().closeDriver();
}
private void initCamera(SurfaceHolder holder) {
try {
CameraManager.getInstance().openDriver(holder);
CameraManager.getInstance().startPreview();
CameraManager.getInstance().requestAutoFocus();
} catch (Exception e) {
Log.i(TAG, "设置相机失败");
return;
}
}
public static void actionStart(Activity activity) {
Intent intent = new Intent(activity, ScanActivity.class);
activity.startActivity(intent);
}
private SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
if (!isHasSurface) {
isHasSurface = true;
initCamera(surfaceHolder);
}
}
@Override
public void surfacedestroyed(SurfaceHolder surfaceHolder) {
isHasSurface = false;
}
};
}
activity的xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SurfaceView
android:id="@+id/scan_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
我这篇博客是从我一个demo里面抠出来的,所以如果要看就主要看注释和介绍,不然像把这个跑起来。。。我自己没有试。我后面会写一篇二维码扫描的基本使用和原理介绍,那里面有完整的demo。
相关阅读
对称密码算法的加密密钥和解密密钥相同,对于大多数对称密码算法,加解密过程互逆。 通常使用的有 DES 3DES AES ,加解密 特点:
php数据采集 之 关于file_get_contents的使用
file_get_contents 函数的应用注意: 开启 php.ini 配置 中 allow_url_fopen =On;\file_get_contents 中的一些参数: 遇到
Java开发者如何正确的使用String,StringBuffer,StringBu
通过前面的文章,我们知道String类最大的特点是不可变性,这意味着对String类的任何修改都会新生成一个字符串,比如你执行了String类的
JDBC的PreparedStatement启动事务使用批处理executeBa
转自:https://blog.csdn.net/xiong9999/article/details/53258698 JDBC使用MySQL处理大数据的时候,自然而然的想到要使用批处理, 普
加壳工具的使用0x01 前言0x01 加壳简介0x02 ASPack加壳0x03 PE-Armor加壳0x01 前言 这是我对加壳工具的使用的学习记录。0x01 加