Android Camera开发:给摄像头预览界面加个ZoomBar(附完整代码下载)

废话不说了,就是加个seekbar,拖动的话能够调节焦距,让画面变大或缩小。下面是核心程序:

一,camera的布局文件



    

    

        

            
        
        
        

        

        

        
    
    

    

    


其中里面嵌套的LinearLayout就是那个ZoomBar,最外面我用了相对布局,发现相对布局用起来还是很好用的。为了方便以后扩展,Camera的SurfaceView用的帧布局。注意SeekBar的几个参数,其中的progressDrawable是指那个横条的形状,可以直接用个图片,也可以写个xml文件。这里用的是xml,当然用图片很简单。seekbar_style.xml文件如下:

   
   
    
           
               
               
           
       
   


下面的android:thumb是滑动的那个手柄,本来我是写了一个xml文件,名字为thumb.xml如下:




    
    
            

            
        


无奈啥也显示不出来,索性直接找了个粗糙的图片,见谅哈!

二,整个程序的主代码:

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 pictureSizes = myParam.getSupportedPictureSizes();
						//List previewSizes = myParam.getSupportedPreviewSizes();
			//			for(int i=0; i focuseMode = (myParam.getSupportedFocusModes());
			for(int i=0; i

需要注意的有以下几点:

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 focuseMode = (myParam.getSupportedFocusModes());
for(int i=0; iLog.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的源码,并且模仿到自己的代码中!

源码下载链接:http://download.csdn.net/detail/yanzi1225627/6421779

你可能感兴趣的:(Android Camera开发:给摄像头预览界面加个ZoomBar(附完整代码下载))