name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:
icon="@drawable/ic_launcher"
android:
label="@string/app_name"
android:
theme="@style/AppTheme">
android:
name=".MainActivity"
android:
label="@string/title_activity_main"
>
name="android.intent.action.MAIN"/>
name="android.intent.category.LAUNCHER"/>
android:
name=".ShowPicActivity"
android:
label="@string/app_name"
android:
theme="@style/AppTheme"
android:
configChanges="orientation|keyboardHidden"
>
主Activity:
[java]viewplaincopyprint?
1.package com.wwj.finger;
2.
3.import java.io.File;
4.import java.io.FileOutputStream;
5.import java.io.IOException;
6.import java.text.SimpleDateFormat;
7.import java.util.Date;
8.
9.
10.import android.app.Activity;
11.import android.content.Intent;
12.import android.graphics.PixelFormat;
13.import android.hardware.Camera;
14.import android.hardware.Camera.PictureCallback;
15.import android.os.Bundle;
16.import android.os.Environment;
17.import android.view.KeyEvent;
18.import android.view.MotionEvent;
19.import android.view.Surface;
20.import android.view.SurfaceHolder;
21.import android.view.SurfaceHolder.Callback;
22.import android.view.SurfaceView;
23.import android.view.View;
24.import android.view.ViewGroup;
25.import android.widget.Toast;
26.
27./**
28. * Android手指拍照
29. *
30. * @author wwj
31. * @date 2013/4/29
32. */
33.public class MainActivity extends Activity {
34. private View layout;
35. private Camera camera;
36. private Camera.Parameters parameters = null;
37.
38. Bundle bundle = null; // 声明一个Bundle对象,用来存储数据
39.
40. @Override
41. public void onCreate(Bundle savedInstanceState) {
42. super.onCreate(savedInstanceState);
43. // 显示界面
44. setContentView(R.layout.activity_main);
45.
46. layout = this.findViewById(R.id.buttonLayout);
47.
48. SurfaceView surfaceView = (SurfaceView) this
49. .findViewById(R.id.surfaceView);
50. surfaceView.getHolder()
51. .setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
52. surfaceView.getHolder().setFixedSize(176, 144); //设置Surface分辨率
53. surfaceView.getHolder().setKeepScreenOn(true);// 屏幕常亮
54. surfaceView.getHolder().addCallback(new SurfaceCallback());//为SurfaceView的句柄添加一个回调函数
55. }
56.
57. /**
58. * 按钮被点击触发的事件
59. *
60. * @param v
61. */
62. public void btnOnclick(View v) {
63. if (camera !
= null) {
64. switch (v.getId()) {
65. case R.id.takepicture:
66. // 拍照
67. camera.takePicture(null, null, new MyPictureCallback());
68. break;
69. }
70. }
71. }
72.
73. /**
74. * 图片被点击触发的时间
75. *
76. * @param v
77. */
78. public void imageClick(View v) {
79. if (v.getId() == R.id.scalePic) {
80. if (bundle == null) {
81. Toast.makeText(getApplicationContext(), R.string.takephoto,
82. Toast.LENGTH_SHORT).show();
83. } else {
84. Intent intent = new Intent(this, ShowPicActivity.class);
85. intent.putExtras(bundle);
86. startActivity(intent);
87. }
88. }
89. }
90.
91. private final class MyPictureCallback implements PictureCallback {
92.
93. @Override
94. public void onPictureTaken(byte[] data, Camera camera) {
95. try {
96. bundle = new Bundle();
97. bundle.putByteArray("bytes", data); //将图片字节数据保存在bundle当中,实现数据交换
98. saveToSDCard(data); // 保存图片到sd卡中
99. Toast.makeText(getApplicationContext(), R.string.success,
100. Toast.LENGTH_SHORT).show();
101. camera.startPreview(); // 拍完照后,重新开始预览
102.
103. } catch (Exception e) {
104. e.printStackTrace();
105. }
106. }
107. }
108.
109. /**