接近传感器启动时如何使相机拍照?

时间:2016-04-22 19:06:56

标签: android

我很想知道当接近或激活接近传感器时如何激活我的程序中的相机。我已经制作了一个传感器和一个启动相机的相机按钮手机,但我无法找到一种方法使其成为触发时的传感器启动相机并拍摄照片。顺便说一句,我正在使用Android Studio进行此操作。

以下是我的主要活动代码:

package com.adalhub.adalhub.sensor;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import java.io.File;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

TextView proxText;
SensorManager sm;
Sensor proxSensor;


private static String logtag = "CameraApp8";
private static int TAKE_PICTURE = -1;
private Uri imageUri;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button cameraButton = (Button)findViewById(R.id.button_camera);
    cameraButton.setOnClickListener(cameraListener);
    // calls button of camera, when button is clicked the camera decides to got to cameraListener










    sm= (SensorManager) getSystemService(SENSOR_SERVICE);
    proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    proxText= (TextView)findViewById(R.id.proximityTextView);
    // this stuff literaly only makes proximity sensor

    sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
    // this is just the type of prox sensor






}









    // when click button this is triggered
private OnClickListener cameraListener = new OnClickListener()
{
    @Override
    public void onClick(View v) {
        // makes it go into camera mode
        takePhoto(v);
    }

};



private void takePhoto(View v){
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
    imageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);

    if(resultCode == Activity.RESULT_OK){
        Uri selectedImage= imageUri;
        getContentResolver().notifyChange(selectedImage, null);

        ImageView imageView = (ImageView)findViewById(R.id.image_camera);
        ContentResolver cr = getContentResolver();
        Bitmap bitmap;

        try{
            bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
            imageView.setImageBitmap(bitmap);
            Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();

        }catch(Exception e){
            Log.e(logtag, e.toString());
        }
    }
}
















@Override
public void onSensorChanged(SensorEvent event) {

    proxText.setText(String.valueOf(event.values[0]));
    //SET AUTOMATIC PHOTO HERE


}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {



}

}

2 个答案:

答案 0 :(得分:0)

使用相机(或Camera2)API拍摄照片,而无需通过相机应用程序。至于传感器 - 数组中的值是对象的距离(cm)。确定您可以使用的阈值,并在前一个值大于该值并且新值小于该值时拍摄照片。

答案 1 :(得分:0)

首先,如果您想拍摄传感器值的变化,那么您应该创建自己的相机,因为使用相机意图需要用户参与。

其次,Android传感器非常敏感(值几乎每纳秒变化一次)所以拍摄传感器值的变化将是CPU和磁盘密集型操作,而不是像检查更改那样间隔拍照间隔后的传感器值。为此,您可以使用Camera V2 API。 CameraV2 can take picture repeatedly for indefinite time

以下是如何使用相机V2 API的示例: - https://github.com/googlesamples/android-Camera2Basic

最后,如果您尝试检测运动,那么使用接近传感器可能不是一个好主意,因为它具有非常低的范围和捕获角度。您可以查看此repo以进行运动检测和使用传感器: -

https://github.com/hiteshsahu/android-motion-detection/blob/master/src/com/jwetherell/motion_detection/SensorsActivity.java

以Android方式检测动作: -

http://developer.android.com/guide/topics/sensors/sensors_motion.html