不错的帖子:
仿UCWEB界面源码!
Android MP3经典案例
根据奇异高清UI界面源码 添加了一些功能
Android 3D立体图片旋转
------------帖子正文-----------------
原文链接:
拍照界面--main.xml
拍照代码:MainActivity.java
package cn.itcast.picture; import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import android.app.Activity;import android.hardware.Camera;import android.hardware.Camera.PictureCallback;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.RelativeLayout; public class MainActivity extends Activity { private RelativeLayout btnLayout; private SurfaceView sfvShow; private Camera camera; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //设置窗口无标题 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置全屏 setContentView(R.layout.main); initView(); } private void initView() { btnLayout = (RelativeLayout)this.findViewById(R.id.btnLayout); //得到SurfaceView显示控件 ,只有得到它,才能捕获摄像头画面 sfvShow = (SurfaceView)this.findViewById(R.id.sfvShow); sfvShow.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); sfvShow.getHolder().setFixedSize(176, 144); sfvShow.getHolder().setKeepScreenOn(true); //使摄像头一直保持高亮 sfvShow.getHolder().addCallback(new SurfaceCallback()); } private final class SurfaceCallback implements Callback{ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { try{ //打开摄像头 camera = Camera.open(); Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewSize(640, 480); parameters.setPreviewFrameRate(5); parameters.setPictureSize(640, 480); parameters.setJpegQuality(80); camera.setParameters(parameters); camera.setPreviewDisplay(holder); //Log.i("摄像头参数", parameters.flatten()); }catch(Exception e){ e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { //一定要关闭摄像头,因为摄像头只能被一个使用 if(camera != null){ camera.release(); camera = null; } } } //拍照 public void takepicture(View v){ if(camera != null){ switch (v.getId()) { case R.id.btnTakePicture: camera.takePicture(null, null, new MyPictureCallBack()); break; case R.id.btnAutoFocus: //不想得到对焦后的事件,传NULL camera.autoFocus(null); break; default: break; } } } //经过压缩处理后的图片数据 private final class MyPictureCallBack implements PictureCallback{ @Override public void onPictureTaken(byte[] data, Camera camera) { try{ File jpgFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); //输出 流 FileOutputStream outStream = new FileOutputStream(jpgFile); outStream.write(data); outStream.close(); //代表摄像头处理完照片了 camera.startPreview(); //开始预览 }catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ btnLayout.setVisibility(ViewGroup.VISIBLE); return true; //返回true,销毁该事件,不要再往后传递了 } return super.onTouchEvent(event); }}
清单文件:
源码下载: