免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1808 | 回复: 0
打印 上一主题 下一主题

Camera摄像头拍照 .. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-30 17:13 |只看该作者 |倒序浏览
Camera摄像头拍照




请看例子:
main.xml

Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.    android:orientation="vertical"  
  4. 4.    android:layout_width="fill_parent"  
  5. 5.    android:layout_height="fill_parent"  
  6. 6.    >  
  7. 7.    <Button android:layout_width="match_parent" android:text="打开按钮" android:id="@+id/button1" android:layout_height="wrap_content"></Button>  
  8. 8.    <Button android:layout_width="match_parent" android:text="关闭按钮" android:id="@+id/button2" android:layout_height="wrap_content"></Button>  
  9. 9.    <Button android:layout_width="match_parent" android:text="拍照按钮" android:id="@+id/button3" android:layout_height="wrap_content"></Button>  
  10. 10.    <SurfaceView android:id="@+id/mySurfaceView"   
  11. 11.        android:gravity="center_horizontal" android:layout_width="fill_parent"  
  12. 12.        android:layout_height="300px" />  
  13. 13.    <ImageView android:id="@+id/myImageView"  
  14. 14.        android:layout_width="fill_parent" android:layout_height="300px" />  
  15. 15.</LinearLayout>  
  16. <?xml version="1.0" encoding="utf-8"?>
  17. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  18.     android:orientation="vertical"
  19.     android:layout_width="fill_parent"
  20.     android:layout_height="fill_parent"
  21.     >
  22.     <Button android:layout_width="match_parent" android:text="打开按钮" android:id="@+id/button1" android:layout_height="wrap_content"></Button>
  23.     <Button android:layout_width="match_parent" android:text="关闭按钮" android:id="@+id/button2" android:layout_height="wrap_content"></Button>
  24.     <Button android:layout_width="match_parent" android:text="拍照按钮" android:id="@+id/button3" android:layout_height="wrap_content"></Button>
  25.         <SurfaceView android:id="@+id/mySurfaceView"
  26.                 android:gravity="center_horizontal" android:layout_width="fill_parent"
  27.                 android:layout_height="300px" />
  28.         <ImageView android:id="@+id/myImageView"
  29.                 android:layout_width="fill_parent" android:layout_height="300px" />
  30. </LinearLayout>
复制代码
AndroidManifest.xml

Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.      package="com.test"  
  4. 4.      android:versionCode="1"  
  5. 5.      android:versionName="1.0">  
  6. 6.    <uses-sdk android:minSdkVersion="8" />  
  7. 7.  
  8. 8.    <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9. 9.        <activity android:name=".TestActivity"   
  10. 10.                  android:label="@string/app_name">  
  11. 11.            <intent-filter>  
  12. 12.                <action android:name="android.intent.action.MAIN" />  
  13. 13.                <category android:name="android.intent.category.LAUNCHER" />  
  14. 14.            </intent-filter>  
  15. 15.        </activity>  
  16. 16.  
  17. 17.    </application>  
  18. 18.    <uses-permission android:name="android.permission.CAMERA"/>  
  19. 19.    <uses-feature android:name="android.hardware.camera" />      
  20. 20.    <uses-feature android:name="android.hardware.camera.autofocus" />  
  21. 21.</manifest>  
  22. <?xml version="1.0" encoding="utf-8"?>
  23. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  24.       package="com.test"
  25.       android:versionCode="1"
  26.       android:versionName="1.0">
  27.     <uses-sdk android:minSdkVersion="8" />

  28.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  29.         <activity android:name=".TestActivity"
  30.                   android:label="@string/app_name">
  31.             <intent-filter>
  32.                 <action android:name="android.intent.action.MAIN" />
  33.                 <category android:name="android.intent.category.LAUNCHER" />
  34.             </intent-filter>
  35.         </activity>

  36.     </application>
  37.     <uses-permission android:name="android.permission.CAMERA"/>
  38.     <uses-feature android:name="android.hardware.camera" />   
  39.         <uses-feature android:name="android.hardware.camera.autofocus" />
  40. </manifest>
复制代码
TestActivity.java主文件

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

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP