【问题标题】:Take photo with button simulate programmatically like the selfie stick via bluetooth使用按钮以编程方式模拟拍照,就像通过蓝牙的自拍杆一样
【发布时间】:2016-05-03 14:09:45
【问题描述】:

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

} 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)) {

}

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

Instrumentation.sendKeyDownUpSync();

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

有人能做到吗?

【问题讨论】:

  • Android 设备型号数以千计。它们附带数百个预装的相机应用程序,还有数百个可从 Play 商店和其他地方安装。无需为外部应用程序提供任何方式来控制何时拍照。
  • 我尝试模拟硬件音量增大按钮,但不是针对所有可能的应用程序
  • @Tazz 你做到了吗?我面临着类似的问题。
  • @Tazz 我也尝试过你在找到这个之前所做的事情。你找到替代方法了吗?请告诉我们。我现在的尝试是使用 root 权限使应用程序本身成为系统应用程序
  • @WuerfelDev 不,我没有,功能被划伤了

标签: android android-intent android-camera android-bluetooth android-event


【解决方案1】:

你可以使用这个意图来拍照

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

您可以找到更多信息here

要在音量键上调用相机,您只需在 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);
    }
}

我用过,没问题

完整演示:

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");
        }
    }
}

SOURCE

【讨论】:

  • 我想使用默认的相机应用程序,而不是制作不同的活动并打开相机的实例:)。就像我说的,我想模拟可以在大多数安卓相机应用程序中按下的音量增大按钮来拍照
  • android.media.action.IMAGE_CAPTURE" 向内置相机应用请求图像
  • 你检查过我给出的例子吗?
  • 是的,我尝试向媒体发送 KeyEvent.VOLUME_UP,但没有任何反应:)。我的意思是它只会增加音量,不拍照
  • 安德斯,我不需要在音量增大键上调用相机,我需要在音量增大键上触发拍照,就像你说的那样,它 100% 正确,但不是我在找什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2012-03-30
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多