这篇文章主要为大家详细介绍了Android实现静默拍照,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android实现静默拍照功能的具体代码,供大家参考,具体内容如下
1.申请权限(6.0以后要动态申请)
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2.创建相机工具类 CameraPreview:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
//初始化Camera对象
mCamera = camera;
//得到SurfaceHolder对象
mHolder = getHolder();
//添加回调,得到Surface的三个声明周期方法
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
//设置预览方向
mCamera.setDisplayOrientation(90);
//把这个预览效果展示在SurfaceView上面
mCamera.setPreviewDisplay(holder);
//开启预览效果
mCamera.startPreview();
} catch (IOException e) {
// Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (holder.getSurface() == null) {
return;
}
//停止预览效果
mCamera.stopPreview();
//重新设置预览效果
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
3.创建xml文件 (要想用户无感拍照,可把视图宽高调为0.1dp或者隐藏在别的页面之下):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/camera_frame"
android:layout_width="match_parent"
android:layout_height="500dp">
</FrameLayout>
</LinearLayout>
4.主类实现静默拍照:
public class CameraActivity extends BaseActivity{
private FrameLayout cameraFrame;
private Camera mCamera;
private TextView cameraTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
cameraFrame = (FrameLayout) findViewById(R.id.camera_frame);
cameraTv = (TextView) findViewById(R.id.camera_tv);
int numberOfCameras = Camera.getNumberOfCameras();// 获取摄像头个数
//遍历摄像头信息
for (int cameraId = 0; cameraId < numberOfCameras; cameraId++) {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//前置摄像头
mCamera = Camera.open(cameraId);//打开摄像头
}
}
CameraPreview mPreview = new CameraPreview(this, mCamera);
cameraFrame.addView(mPreview);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000); // 设置1秒后自动拍照,可调节
//得到照相机的参数
Camera.Parameters parameters = mCamera.getParameters();
//图片的格式
parameters.setPictureFormat(ImageFormat.JPEG);
//预览的大小是多少
parameters.setPreviewSize(800, 400);
//设置对焦模式,自动对焦
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
//对焦成功后,自动拍照
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
//获取照片
mCamera.takePicture(null, null, mPictureCallback);
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
});
}
@Override
protected void onStart() {
super.onStart();
}
//获取照片中的接口回调
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream fos = null;
String mFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "tt005.png";
//文件
File tempFile = new File(mFilePath);
try {
//
fos = new FileOutputStream(tempFile);
fos.write(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//实现连续拍多张的效果
// mCamera.startPreview();
// if (fos != null) {
// try {
// fos.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
}
};
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:Android实现静默拍照功能
基础教程推荐
猜你喜欢
- iOS开发使用XML解析网络数据 2022-11-12
- Android开发Compose集成高德地图实例 2023-06-15
- Flutter进阶之实现动画效果(三) 2022-10-28
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- iOS开发 全机型适配解决方法 2023-01-14
- IOS获取系统相册中照片的示例代码 2023-01-03
- Android实现短信验证码输入框 2023-04-29
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- Android Compose自定义TextField实现自定义的输入框 2023-05-13