基于Android TextureView与SurfaceTexture实现相机Camera拍照预览与保存照片
来源:互联网 发布:淘宝小号点数是什么 编辑:程序博客网 时间:2024/05/24 04:55
基于Android TextureView与SurfaceTexture实现相机Camera拍照预览与保存照片
写一个简单的例子,实现一个常见的开发功能:拍照功能。
技术路线:通过TextureView的通道获取SurfaceTexture作为相机的预览,然后通过Camera的take方法把相机抓取的byte字节数据转换为Bitmap,然后存放到手机的存储器上,至此,一个最简单的拍照功能完成。
注意,本例如果运行在Android高版本(6.0+,7.0+或更高),需要多写运行时权限申请代码。本例出于功能演示,不再冗余的写这部分代码。只给出最骨干的关键代码逻辑。
布局文件activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_light" tools:context="zhangphil.view.MainActivity"> <zhangphil.view.MyTextureView android:id="@+id/textureView" android:layout_width="300dp" android:layout_height="200dp" android:layout_centerInParent="true" /> <Button android:id="@+id/capture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="拍摄" /></RelativeLayout>
MyTextureView.java:
package zhangphil.view;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.SurfaceTexture;import android.hardware.Camera;import android.os.Environment;import android.util.AttributeSet;import android.util.Log;import android.view.TextureView;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;/** * Created by Phil on 2017/9/13. */public class MyTextureView extends TextureView { public Camera mCamera; public MyTextureView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { mCamera = Camera.open(); this.setSurfaceTextureListener(new SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { try { mCamera.setPreviewTexture(surfaceTexture); mCamera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { mCamera.stopPreview(); mCamera.release(); mCamera = null; return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { } }); } public void take() { if(mCamera!=null) mCamera.takePicture(null, null, mPictureCallback); } Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { mCamera.stopPreview(); new FileSaver(data).save(); } }; private class FileSaver implements Runnable { private byte[] buffer; public FileSaver(byte[] buffer) { this.buffer = buffer; } public void save() { new Thread(this).start(); } @Override public void run() { try { File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "zhangphil.png"); file.createNewFile(); FileOutputStream os = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(os); Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length); bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); bos.flush(); bos.close(); os.close(); Log.d("照片已保存", file.getAbsolutePath()); mCamera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } }}
MainActivity.java:
package zhangphil.view;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity { private MyTextureView textureView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textureView = (MyTextureView) findViewById(R.id.textureView); findViewById(R.id.capture).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textureView.take(); } }); }}
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.CAMERA"/>
代码运行结果:
阅读全文
0 0
- 基于Android TextureView与SurfaceTexture实现相机Camera拍照预览与保存照片
- 玩转Android Camera开发(二):使用TextureView和SurfaceTexture预览Camera 基础拍照demo
- 玩转Android Camera开发(二):使用TextureView和SurfaceTexture预览Camera 基础拍照demo
- 玩转Android Camera开发(二):使用TextureView和SurfaceTexture预览Camera 基础拍照demo
- 玩转Android Camera开发(二):使用TextureView和SurfaceTexture预览Camera 基础拍照demo
- 玩转Android Camera开发(二):使用TextureView和SurfaceTexture预览Camera 基础拍照demo
- 使用TextureView和SurfaceTexture预览Camera 基础拍照demo
- TextureView 实现Camera预览
- android调用系统相机拍照与打开相册选择照片进行截取保存笔记
- Android 中的SurfaceTexture,TextureView, GLsurfaceview的区别与联系详解
- android开发之textureview绑定camera实现拍照功能
- TextureView与Camera
- android相机camera拍照
- Android SurfaceTexture和GLSurfaceView做Camera预览
- Android SurfaceTexture和GLSurfaceView做Camera预览
- Android SurfaceTexture和GLSurfaceView做Camera预览
- android拍照与相册选择照片并保存本地
- 开启相机拍照并获取与到照片已经保存到相册中
- CVE-2016-10190 FFmpeg Http协议 heap buffer overflow漏洞分析及利用
- grunt 根据部署环境配置环境参数
- 右移运算符>>和>>>的区别
- redis4.0.1安装教程
- 有程序员转行到其他行业的吗
- 基于Android TextureView与SurfaceTexture实现相机Camera拍照预览与保存照片
- RTS Threshold
- C# 打开文件所在路径并定位到文件
- idea 普通文件夹 转换成 module
- ###Jsp+Servlet购物商城day04.2:订单模块难点总结。【购物商城最大的难点:【封装一个Order对象】查询封装三张表】
- 欢迎使用CSDN-markdown编辑器
- 排序算法之 快速排序 及其时间复杂度和空间复杂度
- shiro学习之路(1)---初识shiro(Hello Word)
- 获取客户端ip