Welcome!
click me.

分类 开发学习记录 下的文章

最近在做电子系统设计的课。我比较懒,不想搞具体的电路,所以直接去找了arduino的了。后面来描述下操作步骤。

1.在proteus中导入arduino库

下载链接及说明:

https://hanbaoaaa.xyz/index.php/archives/124/k.html

2.绘制如图电路

3.程序


byte numDigits = 4; //数码管位数
byte digitPins[] = {11, 10, 9, 8}; //数码管公共极连接Arduino的引脚
byte segmentPins[] = {A1, A2, A3, A4,A5 , 0, 1, 2}; //数码管a,b,c,d,e,f,g,dp对应引脚
//根据共阴极数码管段码表定义0~9显示的各段开关状态
int numTable[10][8] = {
//1为点亮,0为关闭
//a  b  c  d  e  f  g  dp
{1, 1, 1, 1, 1, 1, 0, 0},     //0
{0, 1, 1, 0, 0, 0, 0, 0},     //1
{1, 1, 0, 1, 1, 0, 1, 0},     //2
{1, 1, 1, 1, 0, 0, 1, 0},     //3
{0, 1, 1, 0, 0, 1, 1, 0},     //4
{1, 0, 1, 1, 0, 1, 1, 0},     //5
{1, 0, 1, 1, 1, 1, 1, 0},     //6
{1, 1, 1, 0, 0, 0, 0, 0},     //7
{1, 1, 1, 1, 1, 1, 1, 0},     //8
{1, 1, 1, 1, 0, 1, 1, 0},     //9
};


void setup()
{
  for (int i = 0; i < 8; i++)
  {
  pinMode(segmentPins[i], OUTPUT); //设置4~11引脚为输出模式
  }
  for(int i=0;i<4;i++){
  pinMode(digitPins[i], OUTPUT); //设置4~11引脚为输出模式
  }
}
char numbuffer[4]={1,3,2,4};
void loop()
{
  long n = analogRead(A0);
  n=100*(1023-n)/n+1;
  numbuffer[0]=n%10;
  numbuffer[1]=n/10%10;
  numbuffer[2]=n/100%10;
  numbuffer[3]=n/1000%10;
  for (int j = 0; j < 4;j++)//扫描数码管
  {
    digitalWrite(digitPins[j], HIGH); //设置a 引脚的电平
    digitalWrite(segmentPins[0], !numTable[numbuffer[j]][0]); //设置a 引脚的电平
    digitalWrite(segmentPins[1], !numTable[numbuffer[j]][1]); //设置b 引脚的电平
    digitalWrite(segmentPins[2], !numTable[numbuffer[j]][2]); //设置c 引脚的电平
    digitalWrite(segmentPins[3], !numTable[numbuffer[j]][3]); //设置d 引脚的电平
    digitalWrite(segmentPins[4], !numTable[numbuffer[j]][4]); //设置e 引脚的电平
    digitalWrite(segmentPins[5], !numTable[numbuffer[j]][5]); //设置f 引脚的电平
    digitalWrite(segmentPins[6], !numTable[numbuffer[j]][6]); //设置g 引脚的电平
    digitalWrite(segmentPins[7], !numTable[numbuffer[j]][7]); //设置dp引脚的电平
//    for (int i = 0; i < 10; i++)//以此显示数字0到9
//    {
//    
delay(2);
//    }
    digitalWrite(digitPins[j], LOW); //设置a 引脚的电平
  }

}

4.说明

  • adc为10bit 范围 0-1023
  • arduino中编译后获取hex等编译生成文件

文件 -》首选项 -》

打开最后那个preference.txt,加入一行 build.path=d:\arduino

路径即为hex文件生成的位置

创建一个 CameraProvider类

package com.example.opencv_cpp_test;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;

import java.nio.ByteBuffer;
import java.util.Arrays;

import static androidx.core.content.ContextCompat.checkSelfPermission;


public class CameraProvider {
    private Activity mContext;
    private String mCameraId;
    private Handler mCameraHandler;
    private CameraDevice mCameraDevice;
    private TextureView mTextureView;
    private CaptureRequest.Builder mPreviewBuilder;
    private Size previewSize;

    ImageReader mImageReader;
    private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
//获取最新的一帧的Image
            Image image = reader.acquireLatestImage();
            Log.e("cam123","onImageAvailable");
            if(image!=null){

                image.close();
            }
        }
    };

    public CameraProvider(Activity mContext) {
        this.mContext = mContext;
        //创建了一个Thread来供Camera运行使用,使用HandlerThread而不使用Thread是因为HandlerThread给我们创建了Looper,不用我们自己创建了。
        HandlerThread handlerThread = new HandlerThread("camera");
        handlerThread.start();
        mCameraHandler = new Handler(handlerThread.getLooper());
    }

    public void initTexture(TextureView textureView) {
        mTextureView = textureView;

        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                openCamera(width, height);
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            }
        });
    }
    private void configureTransform(int viewWidth, int viewHeight) {

        int rotation = Surface.ROTATION_90;
        Matrix matrix = new Matrix();
        RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
        RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
        float centerX = viewRect.centerX();
        float centerY = viewRect.centerY();
        if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
            bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
            matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
            float scale = Math.max(
                    (float) viewHeight / previewSize.getHeight(),
                    (float) viewWidth / previewSize.getWidth());
            matrix.postScale(scale, scale, centerX, centerY);
            matrix.postRotate(90 * (rotation - 2), centerX, centerY);
        } else if (Surface.ROTATION_180 == rotation) {
            matrix.postRotate(180, centerX, centerY);
        }
        mTextureView.setTransform(matrix);
    }
    /**
     * surface ready的时候开启Camera
     *
     * @param width  surface的宽
     * @param height surface的高
     */
    @SuppressLint("MissingPermission")
    private void openCamera(int width, int height) {
        CameraManager cameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);

        try {
            for (String cameraId : cameraManager.getCameraIdList()) {
                //描述相机设备的属性类
                CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
                //获取是前置还是后置摄像头
                Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
                //使用后置摄像头
                if (facing != null && facing == CameraCharacteristics.LENS_FACING_BACK) {
                    StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                    if (map != null) {
                        //previewSize = CameraUtil.getOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height);
                        previewSize=new Size(mTextureView.getWidth(),mTextureView.getHeight());
                        mCameraId = cameraId;
                    }
                }
            }
            mImageReader = ImageReader.newInstance(previewSize.getWidth(), previewSize.getHeight(),
                    ImageFormat.JPEG, 2);
            mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mCameraHandler);
            cameraManager.openCamera(mCameraId, mStateCallback, mCameraHandler);
            configureTransform(previewSize.getWidth(),previewSize.getHeight());
        } catch (CameraAccessException r) {

        }
    }
    /**
     * 状态回调
     */
    private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
        @Override
        public void onOpened(CameraDevice camera) {
            mCameraDevice = camera;
            SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
            surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
            Surface previewSurface = new Surface(surfaceTexture);
            try {
                mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                //如果需要多个surface可以add多个
                mPreviewBuilder.addTarget(previewSurface);
                mPreviewBuilder.addTarget(mImageReader.getSurface());
                //mPreviewBuilder.set(CaptureRequest.O, 270);
                mCameraDevice.createCaptureSession(Arrays.asList(previewSurface,mImageReader.getSurface()), mStateCallBack, mCameraHandler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDisconnected(CameraDevice camera) {
            camera.close();
        }

        @Override
        public void onError(CameraDevice camera, int error) {
            camera.close();
        }
    };

    private CameraCaptureSession.StateCallback mStateCallBack = new CameraCaptureSession.StateCallback() {
        @Override
        public void onConfigured(CameraCaptureSession session) {
            CaptureRequest request = mPreviewBuilder.build();
            try {
                //获取一个Image,one-shot
//                session.capture(request, null, mCameraHandler);
                //开启获取Image,repeat模式
                session.setRepeatingRequest(request, null, mCameraHandler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onConfigureFailed(CameraCaptureSession session) {}
    };
    /**
     * 记得关掉Camera
     */
    public void closeCamera() {
        mCameraDevice.close();
    }
}

 

通过这个类可以把摄像头预览绑定到textureView上,然后的话。记得申请权限。我是 事先申请好了的。

使用的话应该看得懂吧 先 new,再init传入textureView就好了。

然后旋转的设置在configureTransform里面。

关键语句

mPreviewBuilder.addTarget(previewSurface);
                mPreviewBuilder.addTarget(mImageReader.getSurface());
                //mPreviewBuilder.set(CaptureRequest.O, 270);
                mCameraDevice.createCaptureSession(Arrays.asList(previewSurface,mImageReader.getSurface()), mStateCallBack, mCameraHandler);

这里要注意的是,加了两种surface。下面也要相应地加入包含这两种的list

mFaceTrackingHandler.removeMessages(1);
mFaceTrackingHandler.obtainMessage(1, trackingInfo).sendToTarget();

安卓message操作。

优点remove 可以防止消息未被处理的情况。

obtain函数 不用新建消息对象。减少性能开销

;