Android菜鸟日记15-传感器与摄像头

传感器
sensorManager = (SensorManager) getContext().getSystemService(
Context.SENSOR_SERVICE);
//获得传感器控制器[SensorManager]
//getContext()获得环境
//getSystemService(Context.SENSOR_SERVICE);获得系统服务(传感器)

sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
//注册传感器监听器
//(监听器、监听内容、监听精度)
//监听器 既 监听内容发生变化的回调函数
//监听内容
Android提供很很多种传感器 传入监听的具体哪一种传感器
//监听精度 多久监听一次

传感器监听器
实现SensorEventListener接口
复写下面方法
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub

pitch = event.values[SensorManager.DATA_Y];
if (pitch < 0) {
pitch = pitch * -1;
}
Log.v("myTag", "pitch:" + pitch);
if (v != null) {
v.setText("pitch:" + pitch);
}

}
重点是onSensorChanged()
当监听的具体传感器发生变化时,调用的函数。
可以从传入的SensorEvent中取值
SensorEvent.values[对应你想取得值(这个值是要在你监听的传感器上有的)]
SensorManager.DATA_Y 意思是在正拿手机的情况下手机与地面的倾角。


摄像头
surfaceView1=(SurfaceView)findViewById(R.id.surfaceView1);
//使用摄像头是需要使用到SurfaceView
//获得SurfaceView对象
surfaceHolder=surfaceView1.getHolder();
//获得SurfaceView的holder
//对SurfaceView的操作都在holder上进行
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//设置 Type
surfaceHolder.addCallback(this);
//设置回调函数对象
//当SurfaceView创建 销毁是 调用此对象onCreate onDestroy方法
//此函数需要实现SurfaceHolder.Callback接口

回调函数Callback内容
public void surfaceCreated(SurfaceHolder holder) {
//当SurfaceView创建时调用此方法
camera=Camera.open();

try {

Camera.Parameters parameters=camera.getParameters();
//获得camera对象的参数对象
Camera.Size a=parameters.getPreviewSize();
Log.v("myTag", "a.height:"+a.height);
Log.v("myTag", "a.width:"+a.width);
parameters.setPreviewSize(480, 320);
//获得camera对象的预览大小
camera.setPreviewDisplay(surfaceHolder);
//设置camera对象的预览显示,显示在surfaceHolder上。[既SurfaceView]
camera.startPreview();
//设置camera对象的预览开始预览
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("surfaceDestroyed");
camera.stopPreview();
//关闭预览
camera.release();
//释放资源
}
注意:
摄像头默认是横向拍摄的
所以要在AndroidManifest.xml下把该activity布局设置成横向的。
使用摄像头还需要开启权限
在<manifest>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

传感器完整代码
package com.poolo;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class SensorControl extends View implements SensorEventListener {

private SensorManager sensorManager;
float pitch = 0f;
TextView v = null;

public SensorControl(Context context, AttributeSet attrs) {
super(context, attrs);

// create SurfaceHolder
sensorManager = (SensorManager) getContext().getSystemService(
Context.SENSOR_SERVICE);
// registerListene
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
}

private Object getDefaultSensor(int typeOrientation) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub

pitch = event.values[SensorManager.DATA_Y];
if (pitch < 0) {
pitch = pitch * -1;
}
Log.v("myTag", "pitch:" + pitch);
if (v != null) {
v.setText("pitch:" + pitch);
}

}

public void setTextView(TextView v) {
this.v = v;
}
}


摄像头完整代码
package com.lurencun.poolo;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CameraTempActivity extends Activity implements SurfaceHolder.Callback {
/** Called when the activity is first created. */
SurfaceHolder surfaceHolder;
SurfaceView surfaceView1;
Button button1;
ImageView imageView1;
Camera camera;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//设为横向显示。因为摄像头会自动翻转90度,所以如果不横向显示,看到的画面就是翻转的。
//本例中不横向

surfaceView1=(SurfaceView)findViewById(R.id.surfaceView1);

surfaceHolder=surfaceView1.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera=Camera.open();
try {

Camera.Parameters parameters=camera.getParameters();

Camera.Size a=parameters.getPreviewSize();
Log.v("myTag", "a.height:"+a.height);
Log.v("myTag", "a.width:"+a.width);
parameters.setPreviewSize(480, 320);
//设置参数
camera.setPreviewDisplay(surfaceHolder);
//摄像头画面显示在Surface上
camera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("surfaceDestroyed");
camera.stopPreview();
//关闭预览
camera.release();
//释放资源
}

}


2011-9-16
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值