package com.example.knowledgemanagement.helper;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.renderscript.Type;
import java.util.List;
public class CameraHelper implements Camera.PreviewCallback {
public static final String TAG="CameraHelper";
private int width;
private int height;
private int mCameraId;
private Camera mCamera;
private byte[] buffer;
private Camera.PreviewCallback mPreviewCallback;
private SurfaceTexture mSurfaceTexture;
private Context mContext;
public CameraHelper(Context context,int width,int height){
this.mContext = context;
this.width=width;
this.height=height;
mCameraId= Camera.CameraInfo.CAMERA_FACING_BACK;
}
public void switchCamera(){
if(mCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
mCameraId=Camera.CameraInfo.CAMERA_FACING_FRONT;
}else{
mCameraId=Camera.CameraInfo.CAMERA_FACING_BACK;
}
stopPreview();
startPreview(mSurfaceTexture);
}
public int getmCameraId(){
return mCameraId;
}
public void stopPreview(){
if(mCamera!=null){
//预览数据回调接口
mCamera.setPreviewCallback(null);
//停止预览
mCamera.stopPreview();
//释放摄像头
mCamera.release();
mCamera=null;
}
}
public void startPreview(SurfaceTexture surfaceTexture){
stopPreview();
try {
mSurfaceTexture = surfaceTexture;
//获取camera属性
mCamera = Camera.open(mCameraId);
//配置cammera的属性
Camera.Parameters parameters = mCamera.getParameters();
//设置预览数据格式为nv21
parameters.setPreviewFormat(ImageFormat.NV21);
boolean isSupportSize = false;
List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
if (supportedPreviewSize.width == width && supportedPreviewSize.height == height) {
isSupportSize = true;
break;
}
}
if (!isSupportSize) {
Camera.Size size=supportedPreviewSizes.get(0);
width=size.width;
height=size.height;
}
//这里是摄像头的宽高
parameters.setPreviewSize(width,height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
mCamera.setDisplayOrientation(90);
//设置摄像头 图像传感器的角度、方向
mCamera.setParameters(parameters);
buffer=new byte[width*height*3/2];
i420=new byte[width*height*3/2];
//数据缓存区
mCamera.addCallbackBuffer(buffer);
mCamera.setPreviewCallbackWithBuffer(this);
//设置预览画面
mCamera.setPreviewTexture(surfaceTexture);
mCamera.startPreview();
}catch (Exception ex){
ex.printStackTrace();
}
}
public int getWidth(){return width;}
public int getHeight(){return height;}
public void setPreviewCallback(Camera.PreviewCallback previewCallback){
mPreviewCallback=previewCallback;
}
byte[] i420;
private void nv21ToI420(byte[] data){
//y数据
System.arraycopy(data,0,i420,0,width*height);
int index=width*height;
for(int i=width*height;i<data.length;i+=2){
i420[index++]=data[i+1];
}
for(int i=width*height;i<data.length;i+=2){
i420[index++]=data[i];
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if(null!=mPreviewCallback) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Allocation bmData = renderScriptNV21ToRGBA888(mContext, width, height, data);
bmData.copyTo(bitmap);
mPreviewCallback.onPreviewFrame(data,camera);
}
camera.addCallbackBuffer(buffer);
}
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
in.copyFrom(nv21);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
return out;
}
}
踏雪羽翼
- 粉丝: 178
- 资源: 53
最新资源
- SpringBoot+Vue3快速开发平台、自研工作流引擎
- 报表例题答案汇总.zip
- POI 工具类,Excel的快速导入导出,Excel模板导出,Word模板导出,可以仅仅5行代码就可以完成Excel的导入导出,修改导出格式简单粗暴,快速有效,easypoi值得你尝试
- python设计成绩管理系统
- 带有智能视觉功能的智能小车源代码100%好用.zip
- python实现飞机大战游戏
- C#实现桌面提醒工具项目
- SwiftLanguageWeather.zip
- MATLAB Simulink电动汽车转弯制动ABS模型,联合直接横摆力矩DYC 转向制动稳定性控制 软件使用:Matlab Simulink 适用场景:轮毂电机分布式驱动电动汽车防抱死控制ABS
- 风机变桨控制基于FAST与MATLAB SIMULINK联合仿真模型非线性风力发电机的 PID独立变桨和统一变桨控制下仿真模型,对于5WM非线性风机风机进行控制 链接simulink的scope出转速
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈