本文实例讲述了android之camera用法。分享给大家供大家参考。具体如下:
1.关于预览横竖差90度的问题
原因分析
经过查证和实验,可以证实:Android提供的SDK(android.hardware.Camera)里大概不能正常的使用竖屏(portrait layout)加载照相机,当用竖屏模式加载照相机时会产生以下情况:
①. 照相机成像左倾90度(倾斜);
②. 照相机成像长宽比例不对(失比)。
之所以是“大概”,原因是因为可能可以通过一些比较复杂的手段解决。如果以上成立,那为什么竖屏不能正常成像也就很显然了。为什么会产生这样的情况,请看下面的研究分析。
照相机在一般情况下是必须用landscape layout(横屏)的,可以证明,先写一个照相机(只要能preview就行),如果Manifest的activity里不加入android:screenOrientation="landscape",即默认了 android:screenOrientation="portrait"(竖屏),照相机preview时就会出现左倾90度的现象,并且失比。原因是这样的(我推测的),摄像头对照物的映射是Android底层固定了的,以landscape方式为正,并且产生大小为320*480的像,如果换成portrait方式了,摄像头还是产生320*480的像,然后分别对应的放入到一个480*320的屏内,显然会失比,然后根据竖、横屏的规则,就产生了左倾90度的情况。为了进一步证实我对失比原因的推测,我照相机内加载的SurfaceView调成了320*213,比例大概是(320:213)*1.5=(480:320),所成像结果如愿的形成左倾但是没有失比的状况,这就证实了我的想法。
综上可以看出,左倾是因为摄像头映射产生的,而失比是由于像素比例映射产生的。
解决方案
暂无好的解决方案,只能强制横屏,记载代码中加入
暂无好的解决方案,只能强制横屏,记载代码中加入
2.关于拍出来的照片不能正确成像,如绿屏,红绿相间,重叠等
原因分析
有的手机不支持parameter.setPictureSize(width,height)、parameters.setPreviewSize(width,height)方法,为了兼容性建议不设置这两个方法。
附:完整示例代码:
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
39
40
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" > < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" android:layout_weight = "2" > < SurfaceView android:id = "@+id/surfaceView" android:layout_width = "fill_parent" android:layout_height = "fill_parent" /> </ LinearLayout > < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" android:layout_weight = "1" android:background = "@android:color/white" android:layout_gravity = "center" > < ImageButton android:layout_width = "fill_parent" android:layout_height = "80dip" android:id = "@+id/btnTakePicture" android:layout_gravity = "center" android:textSize = "30dip" android:layout_weight = "1" android:src = "@drawable/btn_take_pic" /> < ImageButton android:layout_width = "fill_parent" android:layout_height = "80dip" android:id = "@+id/btnAutoFocus" android:layout_gravity = "center" android:textSize = "30dip" android:layout_weight = "1" android:src = "@drawable/btn_auto_focus" /> </ LinearLayout > </ LinearLayout > |
二、MainActivity拍照核心代码:
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
112
113
114
115
|
package cn.itcast.takepicture; import java.io.File; import java.io.FileOutputStream; import android.app.Activity; import android.content.pm.ActivityInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.PixelFormat; import android.graphics.Bitmap.CompressFormat; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.SurfaceHolder.Callback; import android.widget.ImageButton; public class MainActivity extends Activity { private ImageButton btnTakePicture = null ; private ImageButton btnAutoFocus = null ; private Camera camera = null ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // 设置窗口标题 requestWindowFeature(Window.FEATURE_NO_TITLE); // 横屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 全屏显示 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 当此窗口为用户可见时,保持设备常开,并保持亮度不变。 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.main); SurfaceView surfaceView = (SurfaceView) this .findViewById(R.id.surfaceView); surfaceView.getHolder() .setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceView.getHolder().setFixedSize( 320 , 240 ); // 设置分辨率 surfaceView.getHolder().addCallback( new SurfaceCallback()); btnTakePicture = (ImageButton) findViewById(R.id.btnTakePicture); btnAutoFocus = (ImageButton) findViewById(R.id.btnAutoFocus); btnTakePicture.setOnClickListener(onClickListener); btnAutoFocus.setOnClickListener(onClickListener); } private final View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { if (v == btnTakePicture) { if (camera != null ) camera.takePicture( null , null , new TakePictureCallback()); // 拍照 } else if (v == btnAutoFocus) { if (camera != null ) camera.autoFocus( null ); // 对焦 } } }; private final class SurfaceCallback implements Callback { private boolean preview; // 是否正在预览 @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { try { camera = Camera.open(); Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewFrameRate( 5 ); //每秒5帧 parameters.setPictureFormat(PixelFormat.JPEG); //设置照片的输出格式 parameters.set( "jpeg-quality" , 85 ); //照片质量 camera.setParameters(parameters); camera.setPreviewDisplay(holder); camera.startPreview(); preview = true ; } catch (Exception e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (camera != null ) { if (preview) { camera.stopPreview(); preview = false ; } camera.release(); camera = null ; // 记得释放 } } } private final class TakePictureCallback implements PictureCallback { public void onPictureTaken( byte [] data, Camera camera) { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0 , data.length); Matrix matrix= new Matrix(); //设置缩放 matrix.postScale( 0 .5f, 0 .5f); bitmap=Bitmap.createBitmap(bitmap, 0 , 0 , bitmap.getWidth(), bitmap.getHeight(), matrix, true ); File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg" ); try { FileOutputStream outStream = new FileOutputStream(file); bitmap.compress(CompressFormat.JPEG, 100 , outStream); outStream.close(); camera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } } } |
清单文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "cn.itcast.takepicture" android:versionCode = "1" android:versionName = "1.0" > < application android:icon = "@drawable/icon" android:label = "@string/app_name" > < activity android:label = "@string/app_name" android:name = ".MainActivity" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > < uses-sdk android:minSdkVersion = "7" /> < 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 > |
希望本文所述对大家的Android程序设计有所帮助。