publicclassRecorderextendsAppCompatActivity{ private TextureView mPreview; // For displaying the live camera preview private Camera mCamera; // Object to contact the camera hardware private MediaRecorder mMediaRecorder; // Store the camera's image stream as a video
privateboolean isRecording = false; // Is video being recoded private Button btnRecord; // Button that triggers recording
//id is elements's id to find which elements you choose mPreview = (TextureView)findViewById(R.id.textureView1); btnRecord = (Button)findViewById(R.id.button1);
publicvoidonCaptureClick(View view){ if (isRecording) { // Already recording? Release camera lock for others mMediaRecorder.stop(); releaseMediaRecorder(); mCamera.lock();
isRecording = false; releaseCamera(); mGyroFile.close(); mGyroFile = null; btnRecord.setText("Start"); mStartTime = -1; } else { // Not recording – launch new "thread" to initiate! new MediaPrepareTask().execute(null, null, null); } }
classMediaPrepareTaskextendsAsyncTask<Void, Void, Boolean> { //automatically creates a new thread and runs doInBackground in that thread @Override protected Boolean doInBackground(Void... voids){ //identifying supported image sizes from the camera, finding the suitable height, // setting the bitrate of the video and specifying the destination video file. if(prepareVideoRecorder()) { mMediaRecorder.start(); isRecording = true; } else { releaseMediaRecorder(); returnfalse; } returntrue; }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) privatebooleanprepareVideoRecorder(){ // because mCamera is privated object, so other class can't used mCamera = Camera.open(); if (mCamera == null) { Toast.makeText(this, "没有可用相机", Toast.LENGTH_SHORT).show(); returnfalse; }
Camera.Parameters parameters = mCamera.getParameters(); List mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
// find the optimal image size for the camera Camera.Size optimalSize = getOptimalPreviewSize(mSupportedPreviewSizes,mPreview.getWidth(),mPreview.getHeight()); parameters.setPreviewSize(optimalSize.width,optimalSize.height);
//With the optimal size in hand, we can now set up the camera recorder settings CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
//contact the camera hardware and set up these parameters mCamera.setParameters(parameters); mCamera.startPreview();
try { mCamera.setPreviewTexture(mPreview.getSurfaceTexture()); } catch(IOException e) { Log.e(TAG,"Surface texture is unavailable or unsuitable" + e.getMessage()); returnfalse; } //camera unlock need before new a MediaRecorder object mCamera.unlock(); //set up the media recorder mMediaRecorder = new MediaRecorder();
//maybe video sizes are different from preview stream, so get max supported video sizes List supportedVideoSizes = parameters.getSupportedVideoSizes(); for(int i=0;i Log.d("init", "supportedVideoSize:"+supportedVideoSizes.get(i).height+ "x"+supportedVideoSizes.get(i).width); } profile.videoFrameWidth = supportedVideoSizes.get(0).width; profile.videoFrameHeight = supportedVideoSizes.get(0).height; mMediaRecorder.setVideoSize(profile.videoFrameWidth,profile.videoFrameHeight);
//The SensorManager object manages all sensors on the hardware private SensorManager mSensorManager; private Sensor mGyro; private PrintStream mGyroFile; privatelong mStartTime = -1; private List sensorList; mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
@Override protectedvoidonCreate(Bundle savedInstanceState){ ... //print all sensor's name List sensorNameList = new ArrayList(); sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL); for(Sensor sensor : sensorList) { sensorNameList.add(sensor.getName()+"\r\n"); } Log.d("sensor",sensorNameList.toString());
//register sensor listener for gyro mGyro = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); //fetching the gyroscope sensor and registering that this class should receive events (registerListener) mSensorManager.registerListener(gyro_listener, mGyro, SensorManager.SENSOR_DELAY_FASTEST); }
2. 数据打印流初始化
//initialize the PrintStream try { //获取陀螺仪文件输出路径 mGyroFile = new PrintStream(getOutputGyroFile()); mGyroFile.append("gyro\n"); } catch(IOException e) { Log.d(TAG, "Unable to create acquisition file"); returnfalse; } ```
#### 3. 监听传感器数据 ``` java private SensorEventListener gyro_listener = new SensorEventListener(){ @Override publicvoidonAccuracyChanged(Sensor sensor, int accuracy){ // Empty on purpose // Required because we implement SensorEventListener }