Android快速实现无预览拍照功能
作者:-Yee- 时间:2023-12-23 12:23:38
本文实例为大家分享了Android实现无预览拍照功能的具体代码,供大家参考,具体内容如下
实现思路:
把预览的SurfaceView的宽高设置为肉眼看不出的值,例如0.1dp,其他的就是自定义相机的标准步骤了!
当然,网上自定义相机的文章多的去了,我是不可能再做老生常谈的事的,在这里我推荐一个很好用的第三方库,也许很多人已经了解过并且用过,没用过的同学可以试试,亲测好用。
废话不多说,先上源码地址
这里简单介绍一下用法:
1、在gradle里添加库的依赖:compile 'com.wonderkiln:camerakit:0.13.1'
2、布局文件代码:
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试"
android:layout_gravity="center_horizontal"/>
<com.wonderkiln.camerakit.CameraView
android:id="@+id/camera"
android:layout_width="0.1dp"
android:layout_height="0.1dp"
android:adjustViewBounds="true"
camerakit:ckFacing="front" />
</LinearLayout>
camerakit:ckFacing="front" 表示用前置摄像头,其他属性请参照官方文档。
注意:宽高不能设置为0,否则不能拍照。
3、Java代码
public class MainActivity extends BaseActivity {
@BindView(R.id.btn_test)
Button btnTest;
@BindView(R.id.camera)
CameraView cameraView;
@BindView(R.id.ll_content)
LinearLayout llContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
@Override
protected void onResume() {
super.onResume();
cameraView.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
cameraView.stop();
super.onPause();
}
private void initView() {
//设置回调
cameraView.addCameraKitListener(new CameraKitEventListener() {
@Override
public void onEvent(CameraKitEvent cameraKitEvent) {
}
@Override
public void onError(CameraKitError cameraKitError) {
}
@Override
public void onImage(CameraKitImage cameraKitImage) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageBitmap(cameraKitImage.getBitmap());
llContent.addView(imageView);
}
@Override
public void onVideo(CameraKitVideo cameraKitVideo) {
}
});
}
@OnClick(R.id.btn_test)
public void onViewClicked() {
//拍照
cameraView.captureImage();
}
}
来源:https://blog.csdn.net/qq_24834241/article/details/83013586
标签:Android,拍照
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 数据结构与算法系列精讲之贪心算法
2023-09-12 17:56:13
![](https://img.aspxhome.com/file/2023/3/65303_0s.jpg)
详解C# ConcurrentBag的实现原理
2022-11-17 02:26:05
![](https://img.aspxhome.com/file/2023/2/76422_0s.jpg)
java使用Filter实现自动登录的方法
2022-09-09 15:46:47
Windows下搭建Flutter开发环境
2023-11-06 01:32:52
![](https://img.aspxhome.com/file/2023/2/138052_0s.jpg)
Android Studio连接手机设备教程
2021-06-10 14:40:23
![](https://img.aspxhome.com/file/2023/0/97850_0s.png)
Android实现蓝牙串口通讯
2023-03-19 09:22:21
基于Android实现点击某个按钮让菜单选项从按钮周围指定位置弹出
2021-09-13 23:03:40
![](https://img.aspxhome.com/file/2023/3/114283_0s.png)
C#文件后缀名的详细介绍
2022-06-03 15:07:41
C#中Linq的去重方式Distinct详解
2022-01-23 03:53:36
![](https://img.aspxhome.com/file/2023/0/86970_0s.png)
C#实现XSL转换的方法
2023-02-02 03:36:41
跨域解决方案Jsonp原理解析
2023-11-07 18:48:05
Android ListView仿微信聊天界面
2023-10-15 04:59:46
![](https://img.aspxhome.com/file/2023/0/122090_0s.jpg)
集合嵌套之ArrayList嵌套ArrayList实例
2021-06-23 07:32:25
Java实现批量下载选中文件功能
2021-09-09 17:57:45
Android SDK Manager解决更新时的问题 :Failed to fetch URL...
2023-03-17 23:23:36
浅谈springcloud常用依赖和配置
2023-11-24 07:50:02
![](https://img.aspxhome.com/file/2023/2/129982_0s.jpg)
Unity时间戳的使用方法
2023-11-11 05:19:00
SpringMVC文件上传中要解决的问题大汇总
2023-09-02 06:09:20
![](https://img.aspxhome.com/file/2023/2/106172_0s.png)
Java实战之基于TCP实现简单聊天程序
2022-09-01 17:09:20
![](https://img.aspxhome.com/file/2023/8/131978_0s.png)
Android 7.0 Nougat不得不知的11项新功能
2021-06-12 22:50:07