Android Camera开发:给摄像头预览界面加个ZoomBar(附完整代码下载)
源码:http://files.cnblogs.com/android100/StandardCamera2013-10-18.zip
废话不说了,就是加个seekbar,拖动的话能够调节焦距,让画面变大或缩小。下面是核心程序:
一,camera的布局文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/BestWish"
- tools:context=".StandardCamera" />
- <RelativeLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <SurfaceView
- android:id="@+id/previewSV"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </FrameLayout>
- <LinearLayout
- android:id="@+id/zoomLayout"
- android:layout_width="wrap_content"
- android:layout_height="40dp"
- android:layout_centerInParent="true"
- android:layout_centerHorizontal="true"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="-"
- android:textColor="#ffffff"
- android:textSize="30dip"/>
- <SeekBar
- android:id="@+id/seekbar_zoom"
- android:layout_width="300dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:progressDrawable="@drawable/seekbar_style"
- android:thumb="@drawable/ic_launcher"
- android:thumbOffset="0dp" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="+"
- android:textColor="#ffffff"
- android:textSize="30dip" />
- </LinearLayout>
- </RelativeLayout>
- <ImageButton
- android:id="@+id/photoImgBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:background="@drawable/photo_img_btn" />
- </LinearLayout>
其中里面嵌套的LinearLayout就是那个ZoomBar,最外面我用了相对布局,发现相对布局用起来还是很好用的。为了方便以后扩展,Camera的SurfaceView用的帧布局。注意SeekBar的几个参数,其中的progressDrawable是指那个横条的形状,可以直接用个图片,也可以写个xml文件。这里用的是xml,当然用图片很简单。seekbar_style.xml文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@android:id/background">
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:startColor="#ff9d9e9d"
- android:centerColor="#ff5a5d5a"
- android:centerY="0.75"
- android:endColor="#ff747674"
- android:angle="270"
- />
- </shape>
- </item>
- </layer-list>
下面的android:thumb是滑动的那个手柄,本来我是写了一个xml文件,名字为thumb.xml如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- 按下状态 -->
- <item android:state_focused="true" android:state_pressed="true"><shape android:shape="oval">
- <gradient android:angle="0" android:centerColor="#FF00FF00" android:endColor="#000000" android:gradientRadius="8" android:startColor="#FFFF0000" android:type="radial" />
- <size android:height="20dip" android:width="20dip"></size>
- </shape></item>
- </selector>
无奈啥也显示不出来,索性直接找了个粗糙的图片,见谅哈!
二,整个程序的主代码:
- package yan.guoqi.camera;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.List;
- import yan.guoqi.rectphoto.R;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.ColorMatrixColorFilter;
- import android.graphics.Matrix;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.hardware.Camera.AutoFocusCallback;
- import android.hardware.Camera.Parameters;
- import android.hardware.Camera.PictureCallback;
- import android.hardware.Camera.PreviewCallback;
- import android.hardware.Camera.ShutterCallback;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Display;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.ImageButton;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- public class StandardCamera extends Activity implements SurfaceHolder.Callback, PreviewCallback{
- private static final String tag="StandardCamera";
- private boolean isPreview = false;
- private SurfaceView mPreviewSV = null; //棰勮SurfaceView
- private SurfaceHolder mySurfaceHolder = null;
- private ImageButton mPhotoImgBtn = null;
- private Camera myCamera = null;
- private Bitmap mBitmap = null;
- private AutoFocusCallback myAutoFocusCallback = null;
- boolean flag = true;
- private SeekBar mZoomBar = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
- Window myWindow = this.getWindow();
- myWindow.setFlags(flag, flag);
- setContentView(R.layout.activity_rect_photo);
- initView();
- mySurfaceHolder = mPreviewSV.getHolder();
- mySurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
- mySurfaceHolder.addCallback(this);
- mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- myAutoFocusCallback = new AutoFocusCallback() {
- public void onAutoFocus(boolean success, Camera camera) {
- // TODO Auto-generated method stub
- if(success)
- {
- Log.i(tag, "myAutoFocusCallback: success...");
- }
- else
- {
- Log.i(tag, "myAutoFocusCallback: 澶辫触浜�?.");
- }
- }
- };
- //添加ZoomBar
- mZoomBar = (SeekBar)findViewById(R.id.seekbar_zoom);
- mZoomBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- public void onStopTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- }
- public void onStartTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- }
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- // TODO Auto-generated method stub
- Parameters p = myCamera.getParameters();
- p.setZoom(progress);
- myCamera.setParameters(p);
- }
- });
- }
- public void initView(){
- mPreviewSV = (SurfaceView)findViewById(R.id.previewSV);
- WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
- Display display = wm.getDefaultDisplay();
- LayoutParams lpSV = mPreviewSV.getLayoutParams();
- lpSV.width = display.getWidth();
- lpSV.height = (int) ((float)display.getHeight()*0.75);
- mPreviewSV.setLayoutParams(lpSV);
- mPhotoImgBtn = (ImageButton)findViewById(R.id.photoImgBtn);
- LayoutParams lp = mPhotoImgBtn.getLayoutParams();
- lp.width = 240;
- lp.height = 240;
- mPhotoImgBtn.setLayoutParams(lp);
- mPhotoImgBtn.setOnClickListener(new PhotoOnClickListener());
- mPhotoImgBtn.setOnTouchListener(new MyOnTouchListener());
- }
- public void surfaceChanged(SurfaceHolder holder, int format, int width,int height)
- {
- // TODO Auto-generated method stub
- Log.i(tag, "SurfaceHolder.Callback:surfaceChanged!");
- initCamera();
- }
- public void surfaceCreated(SurfaceHolder holder)
- {
- // TODO Auto-generated method stub
- myCamera = Camera.open();
- try {
- myCamera.setPreviewDisplay(mySurfaceHolder);
- Log.i(tag, "SurfaceHolder.Callback: surfaceCreated!");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- if(null != myCamera){
- myCamera.release();
- myCamera = null;
- }
- e.printStackTrace();
- }
- }
- public void surfaceDestroyed(SurfaceHolder holder)
- {
- // TODO Auto-generated method stub
- Log.i(tag, "SurfaceHolder.Callback锛歋urface Destroyed");
- if(null != myCamera)
- {
- myCamera.setPreviewCallback(null);
- myCamera.stopPreview();
- isPreview = false;
- myCamera.release();
- myCamera = null;
- }
- }
- public void initCamera(){
- if(isPreview){
- myCamera.stopPreview();
- }
- if(null != myCamera){
- Camera.Parameters myParam = myCamera.getParameters();
- myParam.setPictureFormat(PixelFormat.JPEG);//璁剧疆鎷嶇収鍚庡瓨鍌ㄧ殑鍥剧墖鏍煎紡
- //List<Size> pictureSizes = myParam.getSupportedPictureSizes();
- //List<Size> previewSizes = myParam.getSupportedPreviewSizes();
- // for(int i=0; i<pictureSizes.size(); i++){
- // Size size = pictureSizes.get(i);
- // Log.i(tag, "initCamera:pictureSizes: width = "+size.width+"height = "+size.height);
- // }
- // for(int i=0; i<previewSizes.size(); i++){
- // Size size = previewSizes.get(i);
- // Log.i(tag, "initCamera:鎽勫儚澶存敮鎸佺殑previewSizes: width = "+size.width+"height = "+size.height);
- //
- // }
- myParam.setPictureSize(1280, 960); //
- myParam.setPreviewSize(960, 720); //
- //myParam.set("rotation", 90);
- myCamera.setDisplayOrientation(90);
- List<String> focuseMode = (myParam.getSupportedFocusModes());
- for(int i=0; i<focuseMode.size(); i++){
- Log.i(tag, focuseMode.get(i));
- if(focuseMode.get(i).contains("continuous")){
- myParam.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
- }
- else{
- myParam.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
- }
- }
- //设置mZoomBar的最大值
- mZoomBar.setMax(myParam.getMaxZoom());
- myCamera.setParameters(myParam);
- myCamera.startPreview();
- myCamera.autoFocus(myAutoFocusCallback);
- isPreview = true;
- }
- }
- ShutterCallback myShutterCallback = new ShutterCallback()
- {
- public void onShutter() {
- // TODO Auto-generated method stub
- Log.i(tag, "myShutterCallback:onShutter...");
- }
- };
- PictureCallback myRawCallback = new PictureCallback()
- {
- public void onPictureTaken(byte[] data, Camera camera) {
- // TODO Auto-generated method stub
- Log.i(tag, "myRawCallback:onPictureTaken...");
- }
- };
- PictureCallback myJpegCallback = new PictureCallback()
- {
- public void onPictureTaken(byte[] data, Camera camera) {
- // TODO Auto-generated method stub
- Log.i(tag, "myJpegCallback:onPictureTaken...");
- if(null != data){
- mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//data鏄瓧鑺傛暟鎹紝灏嗗叾瑙f瀽鎴愪綅鍥� myCamera.stopPreview();
- isPreview = false;
- }
- Matrix matrix = new Matrix();
- matrix.postRotate((float)90.0);
- Bitmap rotaBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, false);
- if(null != rotaBitmap)
- {
- saveJpeg(rotaBitmap);
- }
- myCamera.startPreview();
- isPreview = true;
- }
- };
- public class PhotoOnClickListener implements OnClickListener{
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(isPreview && myCamera!=null){
- myCamera.takePicture(myShutterCallback, null, myJpegCallback);
- }
- }
- }
- public void saveJpeg(Bitmap bm){
- String savePath = "/mnt/sdcard/rectPhoto/";
- File folder = new File(savePath);
- if(!folder.exists()) {
- folder.mkdir();
- }
- long dataTake = System.currentTimeMillis();
- String jpegName = savePath + dataTake +".jpg";
- Log.i(tag, "saveJpeg:jpegName--" + jpegName);
- //File jpegFile = new File(jpegName);
- try {
- FileOutputStream fout = new FileOutputStream(jpegName);
- BufferedOutputStream bos = new BufferedOutputStream(fout);
- // Bitmap newBM = bm.createScaledBitmap(bm, 600, 800, false);
- bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
- bos.flush();
- bos.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public class MyOnTouchListener implements OnTouchListener{
- public final float[] BT_SELECTED=new float[]
- { 2, 0, 0, 0, 2,
- 0, 2, 0, 0, 2,
- 0, 0, 2, 0, 2,
- 0, 0, 0, 1, 0 };
- public final float[] BT_NOT_SELECTED=new float[]
- { 1, 0, 0, 0, 0,
- 0, 1, 0, 0, 0,
- 0, 0, 1, 0, 0,
- 0, 0, 0, 1, 0 };
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- if(event.getAction() == MotionEvent.ACTION_DOWN){
- v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
- v.setBackgroundDrawable(v.getBackground());
- }
- else if(event.getAction() == MotionEvent.ACTION_UP){
- v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
- v.setBackgroundDrawable(v.getBackground());
- }
- return false;
- }
- }
- @Override
- public void onBackPressed()
- {
- // TODO Auto-generated method stub
- super.onBackPressed();
- StandardCamera.this.finish();
- }
- class UpdateThread implements Runnable{
- public void run() {
- // TODO Auto-generated method stub
- while(flag){
- if(myCamera!=null && isPreview)
- myCamera.autoFocus(myAutoFocusCallback); //鑷姩鑱氱劍
- myCamera.setOneShotPreviewCallback(StandardCamera.this); //onPreviewFrame閲屼細鎺ュ彈鍒版暟鎹�? myCamera.stopPreview(); //鍋滄棰勮
- flag = false;
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- public void onPreviewFrame(byte[] data, Camera camera) {
- // TODO Auto-generated method stub
- }
- }
需要注意的有以下几点:
1,为了让程序适用不同的手机,onCreate函数里用如下代码初始化SurfaceView的大小,避免以前写死的方法:
public void initView(){
mPreviewSV = (SurfaceView)findViewById(R.id.previewSV);
WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
LayoutParams lpSV = mPreviewSV.getLayoutParams();
lpSV.width = display.getWidth();
lpSV.height = (int) ((float)display.getHeight()*0.75);
mPreviewSV.setLayoutParams(lpSV);
mPhotoImgBtn = (ImageButton)findViewById(R.id.photoImgBtn);
LayoutParams lp = mPhotoImgBtn.getLayoutParams();
lp.width = 240;
lp.height = 240;
mPhotoImgBtn.setLayoutParams(lp);
mPhotoImgBtn.setOnClickListener(new PhotoOnClickListener());
mPhotoImgBtn.setOnTouchListener(new MyOnTouchListener());
}
2,关于ZoomBar的代码片段很简短,如下:
mPhotoImgBtn = (ImageButton)findViewById(R.id.photoImgBtn);
LayoutParams lp = mPhotoImgBtn.getLayoutParams();
lp.width = 240;
lp.height = 240;
mPhotoImgBtn.setLayoutParams(lp);
mPhotoImgBtn.setOnClickListener(new PhotoOnClickListener());
mPhotoImgBtn.setOnTouchListener(new MyOnTouchListener());
}
//添加ZoomBar
mZoomBar = (SeekBar)findViewById(R.id.seekbar_zoom);
mZoomBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
Parameters p = myCamera.getParameters();
p.setZoom(progress);
myCamera.setParameters(p);
}
});
3,在initCamera函数里,查询camera支持的聚焦模式,如果带连续视频聚焦则使用连续视频聚焦,否则使用自动聚焦:
mPhotoImgBtn = (ImageButton)findViewById(R.id.photoImgBtn);
LayoutParams lp = mPhotoImgBtn.getLayoutParams();
lp.width = 240;
lp.height = 240;
mPhotoImgBtn.setLayoutParams(lp);
mPhotoImgBtn.setOnClickListener(new PhotoOnClickListener());
mPhotoImgBtn.setOnTouchListener(new MyOnTouchListener());
}
List<String> focuseMode = (myParam.getSupportedFocusModes());
for(int i=0; i<focuseMode.size(); i++){
Log.i(tag, focuseMode.get(i));
if(focuseMode.get(i).contains("continuous")){
myParam.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
else{
myParam.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
}
4,同样在initCamera函数里,设置ZoomBar的最大值:
//设置mZoomBar的最大值
mZoomBar.setMax(myParam.getMaxZoom());
后续将写专文分析Camera4.0的源码,并且模仿到自己的代码中!
Android Camera开发:给摄像头预览界面加个ZoomBar(附完整代码下载)的更多相关文章
- 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo
杂家前文是在2012年的除夕之夜仓促完成,后来很多人指出了一些问题,琐事缠身一直没有进行升级.后来随着我自己的使用,越来越发现不出个升级版的demo是不行了.有时候就连我自己用这个demo测一些性能. ...
- Android Camera开发:使用GLSurfaceView预览Camera 基础拍照
GLSurfaceView是OpenGL中的一个类,也是可以预览Camera的,而且在预览Camera上有其独到之处.独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就只有使用GLSur ...
- 使用DevExpress的PdfViewer实现PDF打开、预览、另存为、打印(附源码下载)
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- Android 摄像头预览悬浮窗,可拖动,可显示在其他app上方
市面上常见的摄像头悬浮窗,如微信.手机QQ的视频通话功能,有如下特点: 整屏页面能切换到一个小的悬浮窗 悬浮窗能运行在其他app上方 悬浮窗能跳回整屏页面,并且悬浮窗消失 我们探讨过用CameraX打 ...
- 玩转Android Camera开发(四):预览界面四周暗中间亮,仅仅拍摄矩形区域图片(附完整源代码)
杂家前文曾写过一篇关于仅仅拍摄特定区域图片的demo.仅仅是比較简陋.在坐标的换算上不是非常严谨,并且没有完毕预览界面四周暗中间亮的效果,深以为憾.今天把这个补齐了. 在上代码之前首先交代下,这里面存 ...
- Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整(原理:底层SurfaceView+上层绘制ImageView)
Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整实现(原理:底层SurfaceView+上层绘制ImageView) 分类: Android开发 Androi ...
- Android CameraX 打开摄像头预览
目标很简单,用CameraX打开摄像头预览,实时显示在界面上.看看CameraX有没有Google说的那么好用.先按最简单的来,把预览显示出来. 引入依赖 模块gradle的一些配置,使用的Andro ...
- Android 摄像头预览悬浮窗
用CameraX打开摄像头预览,显示在界面上.结合悬浮窗的功能.实现一个可拖动悬浮窗,实时预览摄像头的例子. 这个例子放进了单独的模块里.使用时注意gradle里的细微差别. 操作摄像头,打开预览.这 ...
- Android Camera开发系列(下)——自定义Camera实现拍照查看图片等功能
Android Camera开发系列(下)--自定义Camera实现拍照查看图片等功能 Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 上 ...
随机推荐
- 測试AtomicInteger与普通int值在多线程下的递增操作
日期: 2014年6月10日 作者: 铁锚 Java针对多线程下的数值安全计数器设计了一些类,这些类叫做原子类,当中一部分例如以下: java.util.concurrent.atomic.Atomi ...
- 算法笔记_217:黑洞数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 任意一个5位数,比如:34256,把它的各位数字打乱,重新排列,可以得到一个最大的数:65432,一个最小的数23456.求这两个数字的差,得:41 ...
- EL和OGNL表达式的区分
OGNL是通常要结合Struts 2的标志一起使用,如<s:property value="#xx" /> struts页面中不能单独使用,el可以单独使用 ${ses ...
- Spring解决Hibernate中的懒加载问题
OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自动被 Spring 的事务管理器探测到. <filter> ...
- Response的返回内容类型
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6682514.html 服务器在返回结果给浏览器时,通常需要先设置响应头的contentType属性.那么,c ...
- Java远程方法协议(JRMP)
Java远程方法协议(英语:Java Remote Method Protocol,JRMP)是特定于Java技术的.用于查找和引用远程对象的协议.这是运行在Java远程方法调用(RMI)之下.TCP ...
- 神经网络:caffe特征可视化的代码例子
caffe特征可视化的代码例子 不少读者看了我前面两篇文章 总结一下用caffe跑图片数据的研究流程 deep learning实践经验总结2--准确率再次提升,到达0.8.再来总结一下 之后.想知道 ...
- 〖Linux〗实时更新 hosts 文件的脚本
适用场景: 下载了一个smarthosts的hosts文件,但hosts文件过旧导致一些ip地址已失效无法访问网络. 脚本使用: ./hostsupdate # 直接从 /etc/hosts 中获得需 ...
- 删除sql server用户时报15138错误
问题 在删除sql server中某数据库下的用户时报15138错误,见下图 解决 可用下述sql代码检查该用户的架构信息,然后逐一修正 SELECT s.name FROM sys.schemas ...
- django之异常错误
现象:最近需要抓取一些网页的信息,但发现Python的乱码问题相对Java来说,不太一样.按照以往Java解决中文乱码问题的思路去解决Python乱码,貌似行不通,报错信息: SyntaxError: ...