- 论坛徽章:
- 0
|
Camera摄像头拍照
请看例子:
main.xml
Xml代码- 1.<?xml version="1.0" encoding="utf-8"?>
- 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 3. android:orientation="vertical"
- 4. android:layout_width="fill_parent"
- 5. android:layout_height="fill_parent"
- 6. >
- 7. <Button android:layout_width="match_parent" android:text="打开按钮" android:id="@+id/button1" android:layout_height="wrap_content"></Button>
- 8. <Button android:layout_width="match_parent" android:text="关闭按钮" android:id="@+id/button2" android:layout_height="wrap_content"></Button>
- 9. <Button android:layout_width="match_parent" android:text="拍照按钮" android:id="@+id/button3" android:layout_height="wrap_content"></Button>
- 10. <SurfaceView android:id="@+id/mySurfaceView"
- 11. android:gravity="center_horizontal" android:layout_width="fill_parent"
- 12. android:layout_height="300px" />
- 13. <ImageView android:id="@+id/myImageView"
- 14. android:layout_width="fill_parent" android:layout_height="300px" />
- 15.</LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button android:layout_width="match_parent" android:text="打开按钮" android:id="@+id/button1" android:layout_height="wrap_content"></Button>
- <Button android:layout_width="match_parent" android:text="关闭按钮" android:id="@+id/button2" android:layout_height="wrap_content"></Button>
- <Button android:layout_width="match_parent" android:text="拍照按钮" android:id="@+id/button3" android:layout_height="wrap_content"></Button>
- <SurfaceView android:id="@+id/mySurfaceView"
- android:gravity="center_horizontal" android:layout_width="fill_parent"
- android:layout_height="300px" />
- <ImageView android:id="@+id/myImageView"
- android:layout_width="fill_parent" android:layout_height="300px" />
- </LinearLayout>
复制代码 AndroidManifest.xml
Xml代码- 1.<?xml version="1.0" encoding="utf-8"?>
- 2.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
- 3. package="com.test"
- 4. android:versionCode="1"
- 5. android:versionName="1.0">
- 6. <uses-sdk android:minSdkVersion="8" />
- 7.
- 8. <application android:icon="@drawable/icon" android:label="@string/app_name">
- 9. <activity android:name=".TestActivity"
- 10. android:label="@string/app_name">
- 11. <intent-filter>
- 12. <action android:name="android.intent.action.MAIN" />
- 13. <category android:name="android.intent.category.LAUNCHER" />
- 14. </intent-filter>
- 15. </activity>
- 16.
- 17. </application>
- 18. <uses-permission android:name="android.permission.CAMERA"/>
- 19. <uses-feature android:name="android.hardware.camera" />
- 20. <uses-feature android:name="android.hardware.camera.autofocus" />
- 21.</manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.test"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="8" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".TestActivity"
- android:label="@string/app_name">
- <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"/>
- <uses-feature android:name="android.hardware.camera" />
- <uses-feature android:name="android.hardware.camera.autofocus" />
- </manifest>
复制代码 TestActivity.java主文件
Java代码- 1.package com.test;
- 2.
- 3.import java.io.IOException;
- 4.
- 5.import android.app.Activity;
- 6.import android.graphics.Bitmap;
- 7.import android.graphics.BitmapFactory;
- 8.import android.graphics.PixelFormat;
- 9.import android.hardware.Camera;
- 10.import android.hardware.Camera.PictureCallback;
- 11.import android.hardware.Camera.ShutterCallback;
- 12.import android.os.Bundle;
- 13.import android.util.DisplayMetrics;
- 14.import android.view.SurfaceHolder;
- 15.import android.view.SurfaceHolder.Callback;
- 16.import android.view.SurfaceView;
- 17.import android.view.View;
- 18.import android.view.View.OnClickListener;
- 19.import android.view.Window;
- 20.import android.view.WindowManager;
- 21.import android.widget.Button;
- 22.import android.widget.ImageView;
- 23.
- 24.public class TestActivity extends Activity implements Callback, OnClickListener {
- 25. /** Called when the activity is first created. */
- 26. SurfaceView mySurfaceView;// SurfaceView的引用
- 27. SurfaceHolder mySurfaceHolder;// SurfaceHolder的引用
- 28. Button button1;// 打开按钮
- 29. Button button2;// 关闭按钮
- 30. Button button3;// 拍照按钮
- 31. Camera myCamera;// Camera的引用
- 32. boolean isView = false;// 是否在浏览中
- 33. @Override
- 34. public void onCreate(Bundle savedInstanceState) {
- 35. super.onCreate(savedInstanceState);
- 36. //设置全屏显示
- 37. requestWindowFeature(Window.FEATURE_NO_TITLE);
- 38. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- 39. WindowManager.LayoutParams.FLAG_FULLSCREEN);
- 40. setContentView(R.layout.main);
- 41. button1 = (Button) findViewById(R.id.button1);
- 42. button2 = (Button) findViewById(R.id.button2);
- 43. button3 = (Button) findViewById(R.id.button3);
- 44. button1.setOnClickListener(this);
- 45. button2.setOnClickListener(this);
- 46. button3.setOnClickListener(this);
- 47. mySurfaceView = (SurfaceView) findViewById(R.id.mySurfaceView);
- 48. mySurfaceHolder = mySurfaceView.getHolder(); //此对象用于在Camera和SurfaceView之间传递数据
- 49. mySurfaceHolder.addCallback(this);
- 50. mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- 51. }
- 52.
- 53. public void initCamera() { //初始化相机
- 54. if (!isView) {
- 55. myCamera = Camera.open();
- 56. }
- 57. if (myCamera != null && !isView) {
- 58. try {
- 59. Camera.Parameters myParameters = myCamera.getParameters();
- 60. myParameters.setPictureFormat(PixelFormat.JPEG); //设置照片格式
- 61. myParameters.set("orientation", "portrait");
- 62.// myParameters.setPreviewSize(dm.widthPixels/2,dm.heightPixels/2); //大小
- 63. myCamera.setParameters(myParameters);
- 64. myCamera.setPreviewDisplay(mySurfaceHolder);
- 65. myCamera.startPreview();
- 66. } catch (IOException e) {
- 67. // TODO Auto-generated catch block
- 68. e.printStackTrace();
- 69. }
- 70. isView = true;
- 71. }
- 72. }
- 73. @Override
- 74. public void onClick(View v) {
- 75. // TODO Auto-generated method stub
- 76. if(v==button1){
- 77. initCamera();
- 78. }else if(v==button2){
- 79. isView=false;
- 80. myCamera.stopPreview();
- 81. myCamera.release();
- 82. myCamera=null;
- 83. }else if(v==button3){
- 84. myCamera.takePicture(mShutterCallback, myRawCallback, myjpegCallback); //进行照相
- 85. }
- 86. }
- 87.
- 88. ShutterCallback mShutterCallback = new ShutterCallback() {
- 89. @Override
- 90. public void onShutter() {
- 91. // TODO Auto-generated method stub
- 92. }
- 93. };
- 94.
- 95. PictureCallback myRawCallback = new PictureCallback() {
- 96. @Override
- 97. public void onPictureTaken(byte[] data, Camera camera) {
- 98. // TODO Auto-generated method stub
- 99. }
- 100. };
- 101. PictureCallback myjpegCallback = new PictureCallback() {
- 102. @Override
- 103. public void onPictureTaken(byte[] data, Camera camera) { //将照下来的图片用ImageView显示
- 104. // TODO Auto-generated method stub
- 105. Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
- 106. ImageView myImageView=(ImageView) findViewById(R.id.myImageView);
- 107. myImageView.setImageBitmap(bm);
- 108. isView=false;
- 109. myCamera.stopPreview();
- 110. myCamera.release();
- 111. myCamera=null;
- 112. initCamera();
- 113. }
- 114. };
- 115.
- 116. @Override
- 117. public void surfaceChanged(SurfaceHolder holder, int format, int width,
- 118. int height) {
- 119. // TODO Auto-generated method stub
- 120. }
- 121.
- 122. @Override
- 123. public void surfaceCreated(SurfaceHolder holder) {
- 124. // TODO Auto-generated method stub
- 125. }
- 126.
- 127. @Override
- 128. public void surfaceDestroyed(SurfaceHolder holder) {
- 129. // TODO Auto-generated method stub
- 130. }
- 131.}
复制代码 |
|