1

I have been trying to create an Android camera activity to test how it works on the emulator, but I am not sure if I am doing things right.

I have added the permission to the manifest for the deprecated camera version, focus and front camera. And I have been looking up tutorials and learning the code.

  1. I have also tried to include a frame layout preview with some custom buttons, but I really don't know how to make the buttons layout overlay the frame.

  2. Do I need to use fragments?

  3. Also I should mention I have read about the new "camera2" and my interest to implement it to the same activity, but maybe that would be just too much for a simple test. What are your recommendations on this?

4

2 回答 2

0

我不认为我可以给你一个确切的答案,因为这是非常愚蠢的回答。但我会尝试为您的方法提供一些高级指导。

通常使用 Fragments 是编写 Android 应用程序的最佳方式,现在它是推荐的方式。

您可以使用 Android 中的 Intents 让另一个应用程序代表您的应用程序工作。您可以启动安装在设备上的相机应用程序以代表您的应用程序拍摄图像。

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

你可以从这里阅读更多

但是,如果您想编写自己的自定义相机 API,这些相机 API 就会发挥作用。因为在某些情况下,您实际上需要自己的自定义相机功能来满足您正在构建的应用程序的特殊要求。

所以决定你想要实现什么。如果您只想获取一张照片并保存它,您可以简单地使用 Intent 并请求相机应用程序为您的应用程序拍摄照片。

要更深入地了解相机 API,您可以从这里开始

编辑,

是的,您实际上可以执行以下操作

if (Build.VERSION.SDK_INT > 9) { 

并且根据版本有不同的执行路径。

于 2015-08-29T08:27:48.903 回答
0

如果您搜索代码,请尝试以下示例代码(我在如何将手机摄像头捕获的图像从一个活动中移动到另一个活动中的图像视图中也有答案?,您可以看看)。

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

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        mFileUri = Uri.fromFile(getOutputMediaFile(1));

        intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);

        // start the image capture Intent
        startActivityForResult(intent, 100);
    }

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

        if (resultCode == RESULT_OK) {
            if (mFileUri != null) {
                // do something...
            }
        }            
    }

    private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == 1) { // image
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
        } else if (type == 2) { // video
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

如果使用模拟器进行测试,请确保相机支持如下:

在此处输入图像描述

于 2015-08-29T08:07:35.700 回答