通过按钮以编程方式模拟拍照,就像通过蓝牙使用自拍杆一样

Taz*_*azz 5 android android-intent android-camera android-event android-bluetooth

所以我有一个蓝牙设备,我听设备上的按钮按下,并尝试在按下按钮时拍照。问题是我没有找到任何解决方案来做到这一点。

} else if (destination.equals(APPLICATION_ACTION_DESTINATION_OPEN_CAMERA)) {
    Intent intent1 = new Intent("android.intent.action.CAMERA_BUTTON");
    intent1.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, KeyEent.KEYCODE_CAMERA));
    context.sendOrderedBroadcast(intent1, null);

    intent1 = new Intent("android.intent.action.CAMERA_BUTTON");
    intent1.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(1, KeyEvent.KEYCODE_CAMERA));
    context.sendOrderedBroadcast(intent1, null);
} else if (destination.equals(APPLICATION_ACTION_DESTINATION_TAKE_PHOTO)) {

}
Run Code Online (Sandbox Code Playgroud)

我发现做到这一点的唯一方法是使用:

Instrumentation.sendKeyDownUpSync();
Run Code Online (Sandbox Code Playgroud)

问题是我需要注册 INJECT_EVENTS 权限,该权限仅授予系统应用程序。

有人设法做到这一点吗?

Max*_*Max 0

您可以使用此意图来拍照

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
Run Code Online (Sandbox Code Playgroud)

您可以在这里找到更多信息

要在音量键上调用相机,您所需要做的就是在 Activity 中重写此方法

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}
Run Code Online (Sandbox Code Playgroud)

我用过,效果完美

完整演示:

public class CameraDemoActivity extends Activity {
    int TAKE_PHOTO_CODE = 0;
    public static int count = 0;

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

        // Here, we are making a folder named Pitures to store
        // pics taken by the camera using this application

        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Pitures/";
        File newdir = new File(dir);
        newdir.mkdirs();

        Button capture = (Button) findViewById(R.id.btnCapture);
        capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Here, the counter will be incremented each time, and the
                // picture taken by camera will be stored as 1.jpg,2.jpg and so on
                count++;
                String file = dir+count+".jpg";
                File newfile = new File(file);
                try {
                    newfile.createNewFile();
                }
                catch (IOException e)
                {
                }

                Uri outputFileUri = Uri.fromFile(newfile);

                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
            }
        });
    }

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

        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
            Log.d("CameraDemo", "Pic saved");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

来源