本文实例为大家讲解如何轻松实现android手机拍照功能,分享给大家供大家参考。具体如下:
一、布局文件main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<?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" >
<surfaceview
android:id= "@+id/surfaceview"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" />
<relativelayout
android:id= "@+id/buttonlayout"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:visibility= "gone" >
<button
android:id= "@+id/takepicture"
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/autofocus"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_aligntop= "@id/takepicture"
android:layout_marginright= "20dp"
android:layout_toleftof= "@id/takepicture"
android:onclick= "takepicture"
android:text= "@string/autofocus" />
</relativelayout>
</framelayout>
|
二、mainactivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
import java.io.file;
import java.io.fileoutputstream;
import android.app.activity;
import android.hardware.camera;
import android.hardware.camera.picturecallback;
import android.os.bundle;
import android.os.environment;
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;
public class mainactivity extends activity {
private view layout;
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);
layout = this .findviewbyid(r.id.buttonlayout);
surfaceview surfaceview = (surfaceview) this
.findviewbyid(r.id.surfaceview);
surfaceview.getholder()
.settype(surfaceholder.surface_type_push_buffers);
surfaceview.getholder().setfixedsize( 176 , 144 );
surfaceview.getholder().setkeepscreenon( true );
surfaceview.getholder().addcallback( new surfacecallback());
}
public void takepicture(view v) {
if (camera != null ) {
switch (v.getid()) {
case r.id.takepicture:
camera.takepicture( null , null , new mypicturecallback());
break ;
case r.id.autofocus:
camera.autofocus( null );
break ;
}
}
}
private final class mypicturecallback implements picturecallback {
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 (exception e) {
e.printstacktrace();
}
}
}
private final class surfacecallback implements callback {
public void surfacecreated(surfaceholder holder) {
try {
camera = camera.open(); // 打开摄像头
camera.parameters parameters = camera.getparameters();
// log.i("mainactivity", parameters.flatten());
parameters.setpreviewsize( 800 , 480 );
parameters.setpreviewframerate( 5 );
parameters.setpicturesize( 1024 , 768 );
parameters.setjpegquality( 80 );
camera.setparameters(parameters);
camera.setpreviewdisplay(holder);
camera.startpreview(); // 开始预览
} catch (exception e) {
e.printstacktrace();
}
}
public void surfacechanged(surfaceholder holder, int format, int width,
int height) {
}
public void surfacedestroyed(surfaceholder holder) {
if (camera != null ) {
camera.release();
camera = null ;
}
}
}
@override
public boolean ontouchevent(motionevent event) {
if (event.getaction() == motionevent.action_down) {
layout.setvisibility(viewgroup.visible);
return true ;
}
return super .ontouchevent(event);
}
}
|
三、添加权限
1
2
3
4
5
|
<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" />
|
效果如下:
希望本文所述对大家学习android软件编程有所帮助。