对于所有的Andr​​oid设备的摄像头应用程序 [英] Camera application for all Android devices

查看:83
本文介绍了对于所有的Andr​​oid设备的摄像头应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发上所发生的一些问题,为Android摄像头应用程序。我需要它在所有Android设备上工作,因为所有这些作品以不同的方式与专用摄像头硬件的,我有一个很难找到一个解决方案,适用于所有的设备。

I'm currently developing a camera application for Android on which some problems have occurred. I need it to work on all Android devices and since all of these works in different ways specially with the camera hardware, I'm having a hard time finding a solution that works for every device.

我的应用程序的主要目标是发射按钮上点击相机,拍一张照片,并将其上传到服务器。所以,我并不真的需要保存图像的设备上的功能,但如果这需要进一步的图像用我还不如允许它。

My application main goal is to launch the camera on a button click, take a photo and upload it to a server. So I don't really need the functionality of saving the image on the device, but if that's needed for further image use I might as well allow it.

例如我测试我对三星Galaxy SII和摩托罗拉垫的应用程序。我得到了工作code表示启动摄像头,这是对了C#code,因为我使用Monodroid:

For example I'm testing my application on a Samsung Galaxy SII and a Motorola Pad. I got working code that launches the camera, which is by the way C# code since I'm using Monodroid:

Intent cameraIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);  
StartActivityForResult(cameraIntent, PHOTO_CAPTURE);

和我获取的结果,类似于本指南我如下: <一href="http://kevinpotgieter.word$p$pss.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/" rel="nofollow">http://kevinpotgieter.word$p$pss.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/ 为什么我遵循这个指南是因为该活动将返回我的星系设备(另一种设备为导向的问题)。

And I fetch the result, similar to this guide I followed: http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/ Why I followed this guide is because the activity returns null on my galaxy device (Another device oriented problem).

这code工作正常银河设备上。它拍照并保存照片的,我可以上传到服务器的画廊。通过进一步的研究,这是明显星系标准的行为,因此这不会对我的摩托罗拉垫工作。相机工作正常,但没有图像保存到画廊。

This code works fine on the Galaxy device. It takes a photo and saves the photo in the gallery from which i can upload to a server. By further research this is apparently galaxy standard behaviour, so this doesn't work on my Motorola pad. The camera works fine, but no image is saved to gallery.

所以,在这种背景下,我的问题是,我在这里在正确的道路上?我是否需要保存图像库,以便在应用程序中继续使用?是否有适用于每一个Android设备的任何解决方案,导致这就是我需要的解决方案。

So with this background my question is, am I on the right path here? Do I need to save the image to gallery in order for further use in my application? Is there any solution that works for every Android device, cause that's the solution i need.

感谢您的反馈意见!

推荐答案

要long2know: 是相同的概念适用于Monodroid。我已经看到你与其他一些类似的中联堆栈的文章。不过我不喜欢在那个特定职位的方法,因为它会检查错误,有些设备是很难coded到一个集合。这意味着它可能无法检测bug在未来的设备。因为我不会做维护这个应用程序,我不能让这一点。我发现了一个解决方案,在其他地方,并适应它我的情况,我会发布它下面,如果有人需要它。它的工作原理我的两个设备上,猜测它会工作,为广大的其他设备。感谢您的文章!

To long2know: Yes the same concepts applies to Monodroid. I've already read the stack article you linked among with some other similar. However i don't like the approach in that particular post since it checks for bugs for some devices that are hardcoded into a collection. Meaning it might fail to detect bugs in future devices. Since i won't be doing maintenance on this application, i can't allow this. I found a solution elsewhere and adapted it to my case and i'll post it below if someone would need it. It works on both my devices, guessing it would work for the majority of other devices. Thanks for your post!

解决方案,使您可以拍摄照片和使用,还与使用的图像从画廊的选择。解决方案使用选项菜单中为这些目的,只是为了测试。 (Monodroid code)。

Solution that allows you to snap a picture and use, also with the option of using a image from gallery. Solution uses option menu for these purposes, just for testing. (Monodroid code).

相机code为启发: <一href="http://stackoverflow.com/questions/8068156/access-to-full-resolution-pictures-from-camera-with-monodroid">access从相机的全分辨率照片与MonoDroid

Camera code is inspired by: access to full resolution pictures from camera with MonoDroid

namespace StackOverFlow.UsingCameraWithMonodroid
{
    [Activity(Label = "ImageActivity")]
    public class ImageActivity
        private readonly static int TakePicture = 1;
        private readonly static int SelectPicture = 2;
        private string imageUriString;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.SetContentView(Resource.Layout.ImageActivity);
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater flate = this.MenuInflater;
            flate.Inflate(Resource.Menu.ImageMenues, menu);

            return base.OnCreateOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.UseExisting:
                    this.SelectImageFromStorage();
                    return true;
                case Resource.Id.AddNew:
                    this.StartCamera();
                    return true;
                default:
                    return base.OnOptionsItemSelected(item);
            }
        }

        private Boolean isMounted
        {
            get
            {
                return Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted);
            }
        }

        private void StartCamera()
        {
            var imageUri = ContentResolver.Insert(isMounted ? MediaStore.Images.Media.ExternalContentUri
                                    : MediaStore.Images.Media.InternalContentUri, new ContentValues());

            this.imageUriString = imageUri.ToString();

            var cameraIntent = new Intent(MediaStore.ActionImageCapture);
            cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
            this.StartActivityForResult(cameraIntent, TakePicture);
        }

        private void SelectImageFromStorage()
        {
            Intent intent = new Intent();
            intent.SetType("image/*");
            intent.SetAction(Intent.ActionGetContent);
            this.StartActivityForResult(Intent.CreateChooser(intent,
                    "Select Picture"), SelectPicture);

        }

        // Example code of using the result, in my case i want to upload in another activity
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            // If a picture was taken
            if (resultCode == Result.Ok && requestCode == TakePicture)
            {
                // For some devices data can become null when using the camera activity.
                // For this reason we save pass the already saved imageUriString to the upload activity
                // in order to adapt to every device. Instead we would want to use the data intent
                // like in the SelectPicture option.

                var uploadIntent = new Intent(this.BaseContext, typeof(UploadActivity));
                uploadIntent.PutExtra("ImageUri", this.imageUriString);
                this.StartActivity(uploadIntent);
            }
            // User has selected a image from storage
            else if (requestCode == SelectPicture)
            {
                var uploadIntent = new Intent(this.BaseContext, typeof(UploadActivity));
                uploadIntent.PutExtra("ImageUri", data.DataString);
                this.StartActivity(uploadIntent);
            }
        }
    }
}

这篇关于对于所有的Andr​​oid设备的摄像头应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆