`
zhoujinhuang
  • 浏览: 91963 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

day3 拍照API

阅读更多

不知道是不是太简单了,官网上没有拍照的例子,网上搜了一圈。

拍照有两种方式,一是使用android.hardware.Camera,二是使用Intent("android.media.action.IMAGE_CAPTURE");

直接使用可以获取打开摄像头,聚焦,预览等事件,并做处理,稍微麻烦一点。用IMAGE_CAPTURE Intent 可以简单的获取拍照的结果,一张图片。

第一种方法 可以看一下 http://www.qqread.com/other-devtool/v472520.html

我用的第二种方法,API做得很傻瓜了。

配置文件,设置访问权限 AndroidManifest.xml

	<uses-permission android:name="android.permission.CAMERA" />
	<uses-feature android:name="android.hardware.camera" />
	<uses-feature android:name="android.hardware.camera.autofocus" />

 

启动照相

		try {
			Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
			startActivityForResult(i, Activity.DEFAULT_KEYS_DIALER);
		} catch (Exception e) {
			Log.e(SimpleCameraActivity.class.getName(), e.getMessage());
		}

  Activity覆盖onActivityResult获取拍照结果,并显示在一个ImageView 中。

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		getWindow().setTitle("onActivityResult" + resultCode);
		super.onActivityResult(requestCode, resultCode, data);
		Bundle extras = data.getExtras();
		Bitmap b = (Bitmap) extras.get("data");
		ImageView img = new ImageView(this);
		img.setImageBitmap(b);
		setContentView(img);
	}

 

先了解一下API,后续怎么用再找资料吧。

 

要注意的问题:

在android2.2的AVD 上运行时,中间会报错"Sorry. the application Camera(process com.andoid.camera) has stopped unexpectedly. Please try again."

日志显示

 

07-01 17:26:42.338: ERROR/AndroidRuntime(452): java.lang.IllegalArgumentException: No configs match configSpec
07-01 17:26:42.338: ERROR/AndroidRuntime(452):     at android.opengl.GLSurfaceView$BaseConfigChooser.chooseConfig(GLSurfaceView.java:760)
07-01 17:26:42.338: ERROR/AndroidRuntime(452):     at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:916)
07-01 17:26:42.338: ERROR/AndroidRuntime(452):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1246)
07-01 17:26:42.338: ERROR/AndroidRuntime(452):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
07-01 17:26:42.688: WARN/ActivityManager(92):   Force finishing activity com.android.camera/.Camera

 

1.5的模拟器没特意加摄像头支持,却不报错,只是提示要SD卡,加了卡的模拟就可以运行了。

2.0的模拟器去掉下面的特性就可以运行。

	<uses-feature android:name="android.hardware.camera" />
	<uses-feature android:name="android.hardware.camera.autofocus" />

 

另外官方文档 http://developer.android.com/guide/developing/tools/emulator.html

说不支持摄像头,但是用1.5的AVD 又可以运行,有点晕。

不清楚什么原因,明天找台2.2的手机装装试试。。。。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics