时间飞逝,转眼间快一个月了。承接上次分享内容,相对于XPC-3399Pro开发板,下位机是基于STM32F103的MCU,固件程序在上期内容中分享出来了,此次用到显示接口HDMI,如果条件允许的话,可以使用双显示接口,而且XPC-3399Pro开发板也是支持双屏输出的。应用场景是柜面收缴假币时,银行工作人员与假币持有人同步观测。不过双屏输出也是有限定的,XPC-3399Pro有三个显示接口,分别是LVDS、HDMI、DP。处理器rk3399有两个GPU,vopb和vopl,vopb最大分辨率支持4K(4096x2160),vopl最大分辨率支持2K(2560x1600)。该开发板在任何时候,只能接入两个显示,且DP与LVDS不能同时使用。因为DP与LVDS在GPU规划为vopb,HDMI规划为vopl 。显示方案有以下两种:
1、HDMI(vopl)+ DP(vopb)
2、HDMI(vopl)+ LVDS(vopb)
如果采用DP接口,使用DP线连接主板的DP接口(CN13)与显示器,然后再上电(暂不支持热插拔)。
上位机是Android系统,因此需要开发一款软件,将灯源切换与摄像头切换整合在一起,实现在各种光源下启用不同摄像机,拍照输出500万像素图片,当然操作期间不可迟滞,否则影响用户体验感。Android系统中编译、调试当然少不了ADB命令行工具。ADB 是一个“客户端-服务器端”程序,其中客户端主要是指PC,服务器端是Android设备的实体机器或者虚拟机。根据PC连接设备的方式不同,ADB 可以分为两类:
①、网络ADB:主机通过有线/无线网络(同一局域网)连接到Android 设备
②、USB ADB:主机通过USB 线连接到Android设备
USB ADB连接步骤如下:
①、连接XPC-3399Pro的micro USB口;
②、先连接好显示设备,给XPC-3399Pro主板上电,运行Android系统。然后依次点击设置--系统--关于本机--系统版本,连续点击7次,打开开发者模式;
③、在Windows CMD中执行:adb devices
开发android应用软件使用的是android studio IDE,基本的功能代码实现,如下图所示:
在android app下,显示的GUI与设备上的硬件按钮一一对应,控制程序使用的是TTL串口通讯,因此设置好对应的跳线帽,使其与下位机直接用TTL串口 通信是关键。尝试用USB ADB方式adb devices,但未查询到对应的设备号,因此光源切换,此次演示就采用硬件按钮。部分参考代码如下:
- package com.demo.u***cameratest3;
- import android.Manifest;
- import android.app.Activity;
- import android.app.ActivityManager;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.pm.PackageManager;
- import android.graphics.Bitmap;
- import android.graphics.SurfaceTexture;
- import android.hardware.u***.U***Device;
- import android.os.Bundle;
- import android.os.Debug;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.os.Vibrator;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.text.format.Formatter;
- import android.util.Log;
- import android.view.Surface;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnLongClickListener;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.widget.CompoundButton;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- import com.serenegiant.common.BaseActivity;
- import com.serenegiant.encoder.MediaMuxerWrapper;
- import com.serenegiant.u***.CameraDialog;
- import com.serenegiant.u***.DeviceFilter;
- import com.serenegiant.u***.USBMonitor;
- import com.serenegiant.u***.USBMonitor.OnDeviceConnectListener;
- import com.serenegiant.u***.USBMonitor.U***ControlBlock;
- import com.serenegiant.u***.UVCCamera;
- import com.serenegiant.u***cameracommon.UVCCameraHandler;
- import com.serenegiant.u***cameratest3.R;
- import com.serenegiant.widget.CameraViewInterface;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import android_serialport_api.SerialPort;
- public final class MainActivity extends BaseActivity implements CameraDialog.CameraDialogParent {
- private static final boolean DEBUG = true; // TODO set false on release
- private static final String TAG = "MainActivity";
- private ArrayList list;
- private ArrayList list0;
- private int i = 20;
- private boolean opencamer = false;
- private boolean one = true;
- /**
- * set true if you want to record movie using MediaSurfaceEncoder
- * (writing frame data into Surface camera from MediaCodec
- * by almost same way as USBCameratest2)
- * set false if you want to record movie using MediaVideoEncoder
- */
- private static final boolean USE_SURFACE_ENCODER = false;
- /**
- * preview resolution(width)
- * if your camera does not support specific resolution and mode,
- * {@link UVCCamera#setPreviewSize(int, int, int)} throw exception
- */
- private static final int PREVIEW_WIDTH = 2592;
- /**
- * preview resolution(height)
- * if your camera does not support specific resolution and mode,
- * {@link UVCCamera#setPreviewSize(int, int, int)} throw exception
- */
- private static final int PREVIEW_HEIGHT = 1944;
- /**
- * preview mode
- * if your camera does not support specific resolution and mode,
- * {@link UVCCamera#setPreviewSize(int, int, int)} throw exception
- * 0:YUYV, other:MJPEG
- */
- private static final int PREVIEW_MODE = 15;
- /**
- * for accessing USB
- */
- private USBMonitor mUSBMonitor;
- /**
- * Handler to execute camera related methods sequentially on private thread
- */
- private UVCCameraHandler mCameraHandler;
- /**
- * for camera preview display
- */
- private CameraViewInterface mUVCCameraView;
- /**
- * for open&start / stop&close camera preview
- */
- private ToggleButton mCameraButton;
- /**
- * button for start/stop recording
- */
- private ImageButton mCaptureButton;
- private UVCCamera mUVCCamera;
- private Button bt_1, bt_2, bt_3;
- private TextView tv_1;
- private Button linght1, linght2, linght3, linght4, linght5, linght6, linght7, linght8, linght9, linght0, back, light13;
- private LinearLayout bt_layout, linght_layout;
- private Bitmap map;
- private TextView tv1, tv2;
- private Surface mPreviewSurface;
- protected SerialPort mSerialPort;
- protected InputStream mInputStream;
- protected OutputStream mOutputStream;
- private ReadThread mReadThread;
- public static String strData = "";
- public long onetime = 0;
- public long twotime = 0;
- Toast mToast;
- public static Thread receiveThread = null;
- public static boolean isFlagSerial = false;
- private long mLastTime = 0;
- private long mCurTime = 0;
- private float value;
- Vibrator vibrator;
- ProgressDialog pd2 = null;
- @Override
- protected void onCreate(final Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (DEBUG) Log.v(TAG, "onCreate:");
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.activity_main);
- mCameraButton = (ToggleButton) findViewById(R.id.camera_button);
- mCameraButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
- final View view = findViewById(R.id.camera_view);
- view.setOnLongClickListener(mOnLongClickListener);
- mUVCCameraView = (CameraViewInterface) view;
- mUVCCameraView.setAspectRatio(PREVIEW_WIDTH / (float) PREVIEW_HEIGHT);
- coonet();
- vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
- bt_1 = findViewById(R.id.bt_1);
- bt_1.setOnClickListener(mOnClickListener);
- open();
- init();
- }
- private void init() {
- linght1 = findButton(R.id.linght1);
- linght2 = findButton(R.id.linght2);
- linght3 = findButton(R.id.linght3);
- linght4 = findButton(R.id.linght4);
- linght5 = findButton(R.id.linght5);
- linght6 = findButton(R.id.linght6);
- linght7 = findButton(R.id.linght7);
- linght8 = findButton(R.id.linght8);
- mUSBMonitor = new USBMonitor(MainActivity.this, mOnDeviceConnectListener);
- final List filter = DeviceFilter.getDeviceFilters(MainActivity.this, com.serenegiant.uvccamera.R.xml.device_filter);
- list0 = (ArrayList) mUSBMonitor.getDeviceList(filter.get(0));
- list = (ArrayList) mUSBMonitor.getDeviceList(filter.get(0));
- for (int j = 0; j
- final U***Device device=list0.get(j);
- int deName=device.getVendorId();
- switch (deName){
- case 1:
- list.set(0,list0.get(j));
- break;
- case 2:
- if (list.get(0)!=null) {
- list.set(1, list0.get(j));
- }
- break;
- case 3:
- if (list.get(0)!=null&&list.get(1)!=null) {
- list.set(2, list0.get(j));
- }
- break;
- }
- }
- if (list.size() == 0) {
- ToastUtils.showToast(getApplicationContext(), "未连接摄像头");
- }
- storage(getApplicationContext(),MainActivity.this);
- }
- private Button findButton(int id) {
- Button bt = findViewById(id);
- bt.setOnClickListener(mOnClickListener);
- return bt;
- }
- public void coonet() {
- mCameraHandler = UVCCameraHandler.createHandler(this, mUVCCameraView,
- USE_SURFACE_ENCODER ? 0 : 1, PREVIEW_WIDTH, PREVIEW_HEIGHT, PREVIEW_MODE);
- }
- private class ReadThread extends Thread {
- @Override
- public void run() {
- super.run();
- while (!isInterrupted()) {
- int size;
- Log.v("debug", "接收线程已经开启");
- try {
- byte[] buffer = new byte[64];
- if (mInputStream == null)
- return;
- size = mInputStream.read(buffer);
- if (size > 0) {
- onDataReceived(buffer, size);
- }
- } catch (IOException e) {
- e.printStackTrace();
- return;
- }
- }
- }
- }
- public boolean close() {
- Log.i("1111111", "串口关闭失败" + isFlagSerial);
- boolean isClose = false;
- if (isFlagSerial) {
- try {
- if (mInputStream != null) {
- mInputStream.close();
- }
- if (mOutputStream != null) {
- mOutputStream.close();
- }
- isClose = true;
- isFlagSerial = false;//关闭串口时,连接状态标记为false
- Log.i("1111111", "串口关闭");
- } catch (IOException e) {
- e.printStackTrace();
- isClose = false;
- }
- return isClose;
- } else {
- Log.i("1111111", "串口已经关闭");
- }
- return isClose;
- }
- @Override
- protected void onStart() {
- super.onStart();
- if (mUVCCameraView != null)
- mUVCCameraView.onResume();
- if (DEBUG) Log.v(TAG, "onStart:");
- mUSBMonitor = new USBMonitor(MainActivity.this, mOnDeviceConnectListener);
- mUSBMonitor.register();
- }
- @Override
- protected void onStop() {
- if (DEBUG) Log.v(TAG, "onStop:");
- mCameraHandler.close();
- if (mUVCCameraView != null)
- mUVCCameraView.onPause();
- setCameraButton(false);
- ToastUtils.close();
- handler.removeCallbacksAndMessages(null);
- super.onStop();
- }
- @Override
- public void onDestroy() {
- if (DEBUG) Log.v(TAG, "onDestroy:");
- if (mCameraHandler != null) {
- mCameraHandler.release();
- mCameraHandler = null;
- }
- if (mUSBMonitor != null) {
- mUSBMonitor.destroy();
- mUSBMonitor = null;
- }
- close();
- mUVCCameraView = null;
- mCameraButton = null;
- handler.removeCallbacksAndMessages(null);
- super.onDestroy();
- }
- /**
- * 获取当前应用使用的内存大小
- *
- * @return 单位 MB
- */
- private double sampleMemory() {
- ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
- double mem = 0.0D;
- try {
- final Debug.MemoryInfo[] memInfo = activityManager.getProcessMemoryInfo(new int[]{android.os.Process.myPid()});
- if (memInfo.length > 0) {
- final int totalPss = memInfo[0].getTotalPss();
- if (totalPss >= 0) {
- mem = totalPss / 1024.0D;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return mem;
- }
- /**
- * event handler when click camera / capture button
- */
- private final OnClickListener mOnClickListener = new OnClickListener() {
- private byte[] E;
- @Override
- public void onClick(final View view) {
- vibrator.vibrate(30);
- switch (view.getId()) {
- case R.id.bt_1: {
- File fileLift = MediaMuxerWrapper.getCaptureFile(getRootPath(getApplicationContext()), ".jpg");
- //拍照
- if (mCameraHandler.isOpened()) {
- mCameraHandler.captureStill(fileLift.getPath());
- Message message = new Message();
- message.what = 12;
- message.obj = "已拷贝图片至" + fileLift.getPath() + "下";
- handler.sendMessage(message);
- } else {
- Message message = new Message();
- message.what = 13;
- message.obj = "摄像机未开启";
- handler.sendMessage(message);
- }
- }
- break;
- case R.id.back:
- showLight(false);
- break;
- case R.id.linght1:
- E = new byte[]{(byte) 0xAA, 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xB4};
- sendString(E);
- break;
- case R.id.linght2:
- E = new byte[]{(byte) 0xAA, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xB4};
- sendString(E);
- break;
- case R.id.linght3:
- E = new byte[]{(byte) 0xAA, 0x09, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xB4};
- sendString(E);
- break;
- case R.id.linght4:
- E = new byte[]{(byte) 0xAA, 0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xB4};
- sendString(E);
- break;
- case R.id.linght5:
- E = new byte[]{(byte) 0xAA, 0x09, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, (byte) 0xB4};
- sendString(E);
- break;
- case R.id.linght6:
- E = new byte[]{(byte) 0xAA, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, (byte) 0xB4};
- sendString(E);
- break;
- case R.id.linght7:
- E = new byte[]{(byte) 0xAA, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, (byte) 0xB4};
- sendString(E);
- break;
- case R.id.linght8:
- mLastTime = mCurTime;
- mCurTime = System.currentTimeMillis();
- if (mCurTime - mLastTime < 300) {//双击事件
- mCurTime = 0;
- mLastTime = 0;
- handler.removeMessages(1);
- handler.sendEmptyMessage(2);
- } else {//单击事件
- handler.sendEmptyMessageDelayed(1, 310);
- }
- break;
- }
- }
- };
- public static String getRootPath(Context context) {
- // 是否有SD卡
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
- || !Environment.isExternalStorageRemovable()) {
- return context.getExternalCacheDir().getPath(); // 有
- } else {
- return context.getCacheDir().getPath(); // 无
- }
- }
- public static void storage(Context context, Activity activity){
- if(ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
- ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
- }
- }
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case 1:
- byte[] E = new byte[]{(byte) 0xAA, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, (byte) 0xB4};
- sendString(E);
- break;
- case 2:
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setTitle("退出");
- builder.setMessage("确定要退出吗");
- builder.setNegativeButton("算了,不想退出了", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(MainActivity.this, "取消退出!", Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("忍心退出", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- System.exit(0);
- }
- });
- builder.show();
- break;
- case 10:
- if ((!mCameraHandler.isOpened()) && (mUVCCameraView.getSurfaceTexture() != null)) {
- if (mUSBMonitor == null) {
- mUSBMonitor = new USBMonitor(MainActivity.this, mOnDeviceConnectListener);
- mUSBMonitor.register();
- }
- if (list.size() < 2) {
- i = 0;
- mUSBMonitor.requestPermission(list.get(0));
- } else {
- if (i == 20) {
- i = 0;
- mUSBMonitor.requestPermission(list.get(i));
- } else {
- mUSBMonitor.requestPermission(list.get(i));
- }
- }
- handler.removeCallbacks(runnable);
- } else {
- mCameraHandler.close();
- setCameraButton(false);
- if (mUSBMonitor != null) {
- mUSBMonitor.unregister();
- mUSBMonitor.destroy();
- mUSBMonitor = null;
- }
- }
- break;
- case 20:
- if (!mCameraHandler.isOpened()) {
- i = i == 0 ? 1 : 0;
- OpenCamer();
- } else {
- i = i == 0 ? 1 : 0;
- OpenCamer();
- }
- break;
- case 15:
- OpenCamer();
- break;
- case 11:
- ToastUtils.showToast(MainActivity.this, "摄像头已连接");
- break;
- case 12:
- case 13:
- String s = (String) msg.obj;
- ToastUtils.showToast(MainActivity.this, s);
- break;
- case 22:
- if (pd2 != null) {
- pd2.dismiss();
- }
- break;
- case 30:
- open();
- if (list == null) {
- ToastUtils.showToast(getApplicationContext(), "未连接摄像头");
- } else {
- handler.post(runnable);
- }
- Log.i(TAG, "onResume");
- mReadThread = new ReadThread();
- mReadThread.start();
- File cache = getCacheDir();
- deleteDir(cache);
- clearAllCache(getApplicationContext());
- Log.e("error", "当前系统总内存=====" + getAvailMemory(getApplicationContext()));
- Log.e("error", "当前应用占用内存=====" + sampleMemory());
- Log.e("error", "存缓路径=====" + cache);
- break;
- }
- }
- };
- private void OpenCamer() {
- onetime = twotime;
- twotime = System.currentTimeMillis();
- Log.i(TAG, "i========" + i);
- if (list.size() == 0) {
- ToastUtils.showToast(getApplicationContext(), "未连接摄像头");
- } else {
- if (twotime - onetime > 1000) {
- if (!mCameraHandler.isOpened()) {
- mUSBMonitor.requestPermission(list.get(i));
- } else {
- mCameraHandler.close();
- if (list.size()==2) {
- if (i == 2) {
- ToastUtils.showToast(getApplicationContext(), "当前只有两个摄像头,不能切换到第三个摄像头");
- } else {
- mUSBMonitor.requestPermission(list.get(i));
- }
- }else if (list.size() == 1) {
- if (i == 1&& i == 2) {
- ToastUtils.showToast(getApplicationContext(), "当前只有一个摄像头,不能切换到其他摄像头");
- } else {
- mUSBMonitor.requestPermission(list.get(i));
- }
- }else {
- mUSBMonitor.requestPermission(list.get(i));
- }
- setCameraButton(false);
- }
- } else {
- Toast.makeText(getApplicationContext(), "当前操作太频繁,请稍后再试", Toast.LENGTH_SHORT).show();
- }
- }
- }
- private final CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener
- = new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(final CompoundButton compoundButton, final boolean isChecked) {
- switch (compoundButton.getId()) {
- case R.id.camera_button:
- Message message = new Message();
- message.what = 20;
- handler.sendMessage(message);
- break;
- }
- }
- };
- /*
- * capture still image when you long click on preview image(not on buttons)
- */
- private final OnLongClickListener mOnLongClickListener = new OnLongClickListener() {
- @Override
- public boolean onLongClick(final View view) {
- switch (view.getId()) {
- case R.id.camera_view:
- if (mCameraHandler.isOpened()) {
- if (checkPermissionWriteExternalStorage()) {
- mCameraHandler.captureStill();
- }
- return true;
- }
- }
- return false;
- }
- };
- private void setCameraButton(final boolean isOn) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if (mCameraButton != null) {
- try {
- mCameraButton.setOnCheckedChangeListener(null);
- mCameraButton.setChecked(isOn);
- } finally {
- mCameraButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
- }
- }
- }
- }, 0);
- }
- @Override
- protected void onPause() {
- super.onPause();
- ToastUtils.close();
- close();
- mCameraHandler.close();
- if (mUSBMonitor != null) {
- mUSBMonitor.unregister();
- mUSBMonitor.destroy();
- mUSBMonitor = null;
- }
- Log.i(TAG, "onPause");
- }
- @Override
- protected void onResume() {
- super.onResume();
- Message message = new Message();
- message.what=30;
- handler.sendMessage(message);
- }
- private Runnable runnable = new Runnable() {
- @Override
- public void run() {
- Message message = new Message();
- message.what = 10;
- handler.sendMessage(message);
- handler.postDelayed(runnable, 2000);
- }
- };
- private static String byte2hex(byte[] buffer) {
- String h = "";
- for (int i = 0; i < buffer.length; i++) {
- String temp = Integer.toHexString(buffer[i] & 0xFF);
- if (temp.length() == 1) {
- temp = "0" + temp;
- }
- h = h + " " + temp;
- }
- return h;
- }
- public boolean open() {
- boolean isopen = false;
- if (isFlagSerial) {
- return false;
- }
- Log.i("1111111", "串口已经打开,打开失败"+isFlagSerial);
- try {
- mSerialPort = new SerialPort(new File("/dev/ttyS3"), 115200, 0);
- mInputStream = mSerialPort.getInputStream();
- mOutputStream = mSerialPort.getOutputStream();
- isopen = true;
- isFlagSerial = true;
- } catch (IOException e) {
- e.printStackTrace();
- Log.i("1111111", "报错了");
- isopen = false;
- }
- Log.i("1111111","open======>"+open());
- return isopen;
- }
- public void
- sendString(byte[] bytes) {
- Log.i("1111111","isopen======>"+open());
- if (open()) {
- try {
- mOutputStream.write(bytes);
- mOutputStream.flush();
- Log.i("1111111", "发送指令======>"+ Arrays.toString(bytes));
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- open();
- try {
- mOutputStream.write(bytes);
- mOutputStream.flush();
- Log.i("1111111","isopen222222======>"+open());
- Log.i("1111111", "发送指令======>"+ Arrays.toString(bytes));
- } catch (IOException e) {
- e.printStackTrace();
- Log.i("1111111", "发送指令出现异常");
- }
- }
- if (!isFlagSerial) {
- Log.i("1111111", "串口未打开,发送失败");
- return;
- }
- }
- private void startPreview() {
- final SurfaceTexture st = mUVCCameraView.getSurfaceTexture();
- if (st == null) {
- Log.e("aaaa", "mUVCCameraView为空");
- }
- mCameraHandler.startPreview(new Surface(st));
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- }
- });
- }
- private final OnDeviceConnectListener mOnDeviceConnectListener = new OnDeviceConnectListener() {
- @Override
- public void onAttach(final U***Device device) {
- }
- @Override
- public void onConnect(final U***Device device, final U***ControlBlock ctrlBlock, final boolean createNew) {
- if (DEBUG) Log.v(TAG, "onConnect:");
- Message message = new Message();
- message.what = 11;
- handler.sendMessage(message);
- mCameraHandler.open(ctrlBlock);
- startPreview();
- }
- @Override
- public void onDisconnect(final U***Device device, final U***ControlBlock ctrlBlock) {
- if (DEBUG) Log.v(TAG, "onDisconnect:");
- if (mCameraHandler != null) {
- queueEvent(new Runnable() {
- @Override
- public void run() {
- mCameraHandler.close();
- }
- }, 0);
- setCameraButton(false);
- }
- }
- @Override
- public void onDettach(final U***Device device) {
- }
- @Override
- public void onCancel(final U***Device device) {
- setCameraButton(false);
- }
- };
- /**
- * to access from CameraDialog
- *
- * @return
- */
- @Override
- public USBMonitor getUSBMonitor() {
- return mUSBMonitor;
- }
- @Override
- public void onDialogResult(boolean canceled) {
- if (DEBUG) Log.v(TAG, "onDialogResult:canceled=" + canceled);
- if (canceled) {
- setCameraButton(false);
- }
- }
- protected void onDataReceived(final byte[] buffer, final int size) {
- runOnUiThread(new Runnable() {
- public void run() {
- String msg = ByteUtil.bytes2HexString(buffer, size);
- switch (msg) {
- case "AA0100AB":
- Toast.makeText(getApplicationContext(), "返回操作成功", Toast.LENGTH_SHORT).show();
- break;
- case "AA0101AC":
- Toast.makeText(getApplicationContext(), "返回操作失败", Toast.LENGTH_SHORT).show();
- break;
- case "AB03010000AF":
- i = 0;
- OpenCamer();
- break;
- case "AB03000100AF":
- i = 1;
- OpenCamer();
- break;
- case "AB03000001AF":
- if (list.size()<2){
- ToastUtils.showToast(getApplicationContext(),"当前只有两个摄像头");
- }else {
- i = 2;
- OpenCamer();
- }
- break;
- case "AA09000000000000000001B4":
- File fileLift = MediaMuxerWrapper.getCaptureFile(Environment.DIRECTORY_DCIM, ".jpg");
- //拍照
- if (mCameraHandler.isOpened()) {
- mCameraHandler.captureStill(fileLift.getPath());
- Message message = new Message();
- message.what = 12;
- message.obj = "已拷贝图片至" + fileLift.getPath() + "下";
- handler.sendMessage(message);
- } else {
- Message message = new Message();
- message.what = 13;
- message.obj = "摄像机未开启";
- handler.sendMessage(message);
- }
- break;
- default:
- Toast.makeText(getApplicationContext(), "异常操作", Toast.LENGTH_SHORT).show();
- break;
- }
- Log.v("debug", "接收到串口信息======>" + (ByteUtil.bytes2HexString(buffer, size)));
- }
- });
- }
- public void showLight(boolean show) {
- if (show) {
- linght_layout.setVisibility(View.VISIBLE);
- bt_layout.setVisibility(View.GONE);
- } else {
- linght_layout.setVisibility(View.GONE);
- bt_layout.setVisibility(View.VISIBLE);
- }
- }
- public String getAvailMemory(Context context) {
- ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
- am.getMemoryInfo(mi);
- // mi.availMem; 当前系统的可用内存
- return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
- }
- /**
- * 清除缓存
- *
- * @param context
- */
- public void clearAllCache(Context context) {
- deleteDir(context.getCacheDir());
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- deleteDir(context.getExternalCacheDir());
- }
- }
- private boolean deleteDir(File dir) {
- if (dir != null && dir.isDirectory()) {
- String[] children = dir.list();
- for (int i = 0; i < children.length; i++) {
- boolean success = deleteDir(new File(dir, children[i]));
- if (!success) {
- return false;
- }
- }
- }
- return dir.delete();
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- switch (requestCode){
- case 1:
- if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- ToastUtils.showToast(getApplicationContext(),"获取到存储权限");
- }else {
- storage(getApplicationContext(),MainActivity.this);
- ToastUtils.showToast(getApplicationContext(),"拒绝");
- }
- break;
- }
- }
- }
复制代码
操作切换摄像机实时采集图像并无任何迟滞、卡顿的现象,如采用RK3288或者全志A64平台就会明显的迟滞,当然也许全志的更优方案会满足实时性,方案会不会采用,还得看产品的性价比。此次的操作摄像头演示见以下链接,可能表述有些不太清楚,切勿吐槽,谢谢。
https://v.youku.com/v_show/id_XNTE2NTg1OTk5Mg==.html
https://v.youku.com/v_show/id_XNTE2NTg1OTk5Mg==.html
|