如何在 Android 中调用另一个使用自定义相机应用程序拍照的活动?

发布于 2025-01-04 09:21:08 字数 1140 浏览 0 评论 0原文

我在互联网上提供的一些教程的帮助下实现了相机应用程序。

我可以拍照,将它们保存在单独的目录中,并将信息存储在我自己的数据库中。在这种情况下,我不会向用户显示任何图像预览。

现在我的要求是在拍照后显示带有“保存”和“取消”按钮的图像预览。为了实现这一目标,我使用 ImageView 和按钮创建了另一个活动。

在我的主要活动中,我正在执行以下操作。

cameraButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            camera.takePicture(shutterCallback, rawCallback, jpegCallback);
        }
});
........    
PictureCallback jpegCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] imageData, Camera c) {
            Intent intent = new Intent(MyCameraActivity.this, SaveOrDiscardActivity.class);
            intent.putExtra("LOC_ATTRS", new double[]{dLatitude, dLongitude});
            intent.putExtra("IMG_DATA", imageData);
            startActivityForResult(intent, RESULT_OK);
            camera.startPreview();          
        }
    };

在我的 SaveOrDiscardActivity 类中,我获取数据并将其放入 ImageView 中。 我能够正确地看到预览。

现在我有以下问题。

1)从 onPictureTaken 方法调用另一个活动是个好方法吗?

2) 单击相机按钮后,需要一些可测量的时间(2 到 5 秒)才能转到另一个活动。如何解决这个问题?

提前致谢。

I implemented a Camera Application with the help of some of the tutorials available in internet.

And I am able to take pictures, saving them in a separate directory and storing the information in my own database. In this case I am not showing any preview of the Image to the user.

Now my requirement is to show a Image Preview with Save and Cancel buttons after taking the picture. For achieving this, I created another activity with ImageView and Buttons.

And in my main Activity I am doing the following.

cameraButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            camera.takePicture(shutterCallback, rawCallback, jpegCallback);
        }
});
........    
PictureCallback jpegCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] imageData, Camera c) {
            Intent intent = new Intent(MyCameraActivity.this, SaveOrDiscardActivity.class);
            intent.putExtra("LOC_ATTRS", new double[]{dLatitude, dLongitude});
            intent.putExtra("IMG_DATA", imageData);
            startActivityForResult(intent, RESULT_OK);
            camera.startPreview();          
        }
    };

In my SaveOrDiscardActivity class I am getting the data and putting it in the ImageView.
I am able to see the Preview properly.

Now I have the following questions.

1) Is it good approach to call another activity from onPictureTaken method ?

2) After clicking on the Camera Button it is taking some measurable time(2 to 5 seconds) to go to the another activity. How to fix this issue ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毅然前行 2025-01-11 09:21:08

使用以下代码调用相机:

Intent cameraIntent1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

File cameraImagefolder = new File(Environment.getExternalStorageDirectory() + "/phimages/");

if (!cameraImagefolder.isDirectory()) 
{
   cameraImagefolder.mkdirs();
 }  
 File file = new File(cameraimagename);
 Log.d("HI1", "camera image name" + cameraimagename);

 Uri outputFileUri = Uri.fromFile(file);
 cameraIntent1.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
 startActivityForResult(cameraIntent1, CAMERA_REQUEST);

在 onActivityForresult 方法中调用 SaveOrDiscardActivity。

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

      Intent intent = new Intent(MyCameraActivity.this, SaveOrDiscardActivity.class);
            intent.putExtra("LOC_ATTRS", new double[]{dLatitude, dLongitude});
            intent.putExtra("IMG_DATA", imageData);
            startActivityForResult(intent, RESULT_OK);
            camera.startPreview();      }

Invoke Camera by using following code:

Intent cameraIntent1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

File cameraImagefolder = new File(Environment.getExternalStorageDirectory() + "/phimages/");

if (!cameraImagefolder.isDirectory()) 
{
   cameraImagefolder.mkdirs();
 }  
 File file = new File(cameraimagename);
 Log.d("HI1", "camera image name" + cameraimagename);

 Uri outputFileUri = Uri.fromFile(file);
 cameraIntent1.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
 startActivityForResult(cameraIntent1, CAMERA_REQUEST);

Call your SaveOrDiscardActivity in onActivityForresult method.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

      Intent intent = new Intent(MyCameraActivity.this, SaveOrDiscardActivity.class);
            intent.putExtra("LOC_ATTRS", new double[]{dLatitude, dLongitude});
            intent.putExtra("IMG_DATA", imageData);
            startActivityForResult(intent, RESULT_OK);
            camera.startPreview();      }
檐上三寸雪 2025-01-11 09:21:08

在我对 PictureCallback 以及 Activity 进行了一些研究之后,我发现您可能需要根据您的代码进行一些更改。

我假设您需要使用 PictureCallback 将图片数据传递回其父活动,对吗?

如果是这种情况,您可以首先使用 startActivityForResult 调用相机 Activity

,然后在相机 Activity 中使用 PictureCallback 中的 setResult(int resultCode, Intent data) 方法 如:

PictureCallback jpegCallback = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] imageData, Camera c) {
        ...
        if(getParent() == null) {
            setResult(RESULT_OK, data);
        } else {
            getParent().setResult(RESULT_OK, data);
        }
        finish(); 
        ...
// If you don't call finish() here 
//The current activity won't be terminated, you'll stay in the camera preview 
//but not go back to the parent activity.         
    }

为了进一步参考,请查看 Android 开发者官方网站:
捕获图片
活动

如果您遇到家长无法得到结果的问题,请参考这里的答案https://stackoverflow.com/a/2621390/763459

After I did a few research on PictureCallback as well as Activity, I found you might need to do a little bit change based on your code.

I assume that you need to use the PictureCallback to pass the picture data back to its parent activity, right?

If that's the case, you can first call your camera activity use startActivityForResult

Then, within your camera activity, use setResult(int resultCode, Intent data) method within PictureCallback like:

PictureCallback jpegCallback = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] imageData, Camera c) {
        ...
        if(getParent() == null) {
            setResult(RESULT_OK, data);
        } else {
            getParent().setResult(RESULT_OK, data);
        }
        finish(); 
        ...
// If you don't call finish() here 
//The current activity won't be terminated, you'll stay in the camera preview 
//but not go back to the parent activity.         
    }

For your further reference, please check Android Developer's Official Site:
Capture Picture and
Activity

And if you're facing problems that the parent can't get the result, please refer to this answer here https://stackoverflow.com/a/2621390/763459

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文