不错的帖子:
仿UCWEB界面源码!
http://www.eoeandroid.com/thread-225509-1-1.html
Android MP3经典案例
http://www.eoeandroid.com/thread-212478-1-1.html
根据奇异高清UI界面源码 添加了一些功能
http://www.eoeandroid.com/thread-212872-1-1.html
Android 3D立体图片旋转
http://www.eoeandroid.com/thread-212512-1-1.html
------------帖子正文-----------------
原文链接:http://www.eoeandroid.com/thread-205293-1-1.html
拍照界面--main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 父界面 -->
<SurfaceView
android:id="@+id/sfvShow"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- 子界面 -->
<RelativeLayout
android:id="@+id/btnLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" >
<Button
android:id="@+id/btnTakePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:onClick="takepicture"
android:text="@string/takepicture" />
<Button
android:id="@+id/btnAutoFocus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/btnTakePicture"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@id/btnTakePicture"
android:onClick="takepicture"
android:text="@string/autoFocus" />
</RelativeLayout>
</FrameLayout>
拍照代码: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);
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.picture"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 在SDCard中写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
源码下载:takepicture.rar