Monodroid 相机/图像视图示例

发布于 2024-11-05 20:52:00 字数 366 浏览 0 评论 0原文

有人可以发布一个示例,说明如何使用相机、捕获图像、在图像视图中预览图像、将图像压缩为 jpg 并将字节上传到远程服务器吗?我能找到的最接近的如下。我们有相机和图像捕获,但我们需要知道如何预览、将 jpg 压缩/调整为 640/480px 和大约 120kb 大小,然后将字节上传到远程服务器。感谢大家的帮助。 http://android-coding.blogspot.com/2010/12 /intent-of-mediastoreactionimagecapture.html

Can someone post an example of how to use the camera, capture the image, preview the image in an image view, compress the image in jpg and upload the bytes to a remote server? The closest I have been able to find is below. We have the camera, and image capture but we need to know how to preview, compress/resize jpg to 640/480px and around 120kb size then upload bytes to a remote server. Thanks to all of you for your help.
http://android-coding.blogspot.com/2010/12/intent-of-mediastoreactionimagecapture.html

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

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

发布评论

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

评论(2

肤浅与狂妄 2024-11-12 20:52:00

查看您的代码,我发现有些事情是错误的:

-[用于相机功能]-

  1. 不要自己创建文件。这是没有必要的。使用 ContentResolver.Insert 函数返回包含图片的文件 URI,就像这里所做的一样,如果你想检查是否存在外部存储器,也可以接管 isMounted 。
  2. 您正在检查是否有数据,然后检查是否有缩略图。如果没有缩略图,您将获得完整图像。这没有意义,除非你想制作完整版本的拇指,如果拇指没有归还?您是否只想获取完整版本或两者,而不是这个或那个?
  3. 您正在检索字符串值变量以获取完整图像的 URI?只需将从第一点中的代码中获取的 uri 保存为属性(比方说活动类中的“myPhotoURI”)。在处理相机意图结果的 OnActivityResult 函数中,只需回忆该 URI 并按如下方式使用它(是的,您没看错;我什至没有使用数据意图,只是记住了 uri):

    Bitmap imageFromCam = MediaStore.Images.Media.GetBitmap(this.ContentResolver, Android.Net.Uri.Parse(myPhotoURI));

  4. 要从图库中获取图像,只需使用 SelectImageFromStorage() 函数 从这个问题的答案并在 OnActivityResult 检查中检索所选图像的 URI,只需使用:

    Android.Net.Uri selectedImageUri = data.ToURI();

这对我来说就像一个魅力。

-[将数据发送到 Web 服务]-

假设您正在使用 WCF 或 Web 服务,并且希望以字节数组的形式接收图像数据; 此问题的批准答案给出了一个很好的例子,说明如何将图像转换为字节数组(无论如何,这正是 WCF Web 服务想要的),

我认为这些方向将帮助您前进。

Looking at your code there are some things i notice to be wrong:

-[ for the camera functionality ]-

  1. Don't create a file yourself. This is not necessary. Use the ContentResolver.Insert function to give you back a file URI that will contain the picture, just like done here and also take over the isMounted if you want to check if there is external memory present.
  2. You are checking if there's data and then checking if there is a thumbnail. If there's no thumbnail you'll get the full image. That doesn't make sense unless you want to make a thumb of the full version if the thumb is not given back?? Don't you just want to grab the full version or both but not this OR that?
  3. You are retrieving a string value variable to get the URI to the full image? Just save the uri you get from the code in my first point as a property (let's say "myPhotoURI" in the activity class. In the OnActivityResult function that handles your camera intent result just recall that URI and use it as following (Yes, you're seeing it right; i'm not even using the data intent for this just the remembered uri):

    Bitmap imageFromCam = MediaStore.Images.Media.GetBitmap(this.ContentResolver, Android.Net.Uri.Parse(myPhotoURI));

  4. To grab an image from the gallery just use the SelectImageFromStorage() function from this question's answer and retrieve the URI of the chosen image in the OnActivityResult check just use:

    Android.Net.Uri selectedImageUri = data.ToURI();

That's what worked like a charm for me.

-[sending the data to a webservice ]-

Assuming you're using a WCF or webservice that'll want to receive the image data as a byte array; the approved answer to this question gives a nice example of how to convert your image to an byte array (which is what a WCF webservice wants, anyway)

I think that these directions will get you going.

我恋#小黄人 2024-11-12 20:52:00

这是迄今为止最接近的示例...当使用额外输出时,这会返回空数据。仍在尝试访问完整图像/照片而不是缩略图。

    private void saveFullImage() {
        Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);

        string file = System.IO.Path.Combine(Android.OS.Environment.DirectoryDcim.ToString(), "test.jpg");

        var outputFileUri = Android.Net.Uri.Parse(file);
        intent.PutExtra(Android.Provider.MediaStore.ExtraOutput, outputFileUri);
        StartActivityForResult(intent, TAKE_PICTURE);
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_PICTURE)
        {
            Uri imageUri = null;
            // Check if the result includes a thumbnail Bitmap
            if (data != null)
            {
                if (data.HasExtra("data"))
                {
                    var thumbnail = data.GetParcelableArrayExtra("data");
                    // TODO Do something with the thumbnail
                }
            }
            else
            {
                var outputFileUri = data.GetParcelableArrayExtra("outputFileuri");
                // TODO Do something with the full image stored
                // in outputFileUri
            }
        }
    }

here is the closest example to date... this brings back Null data when the extra output is used. Still trying to get access to the full image/photo and not a thumbnail.

    private void saveFullImage() {
        Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);

        string file = System.IO.Path.Combine(Android.OS.Environment.DirectoryDcim.ToString(), "test.jpg");

        var outputFileUri = Android.Net.Uri.Parse(file);
        intent.PutExtra(Android.Provider.MediaStore.ExtraOutput, outputFileUri);
        StartActivityForResult(intent, TAKE_PICTURE);
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_PICTURE)
        {
            Uri imageUri = null;
            // Check if the result includes a thumbnail Bitmap
            if (data != null)
            {
                if (data.HasExtra("data"))
                {
                    var thumbnail = data.GetParcelableArrayExtra("data");
                    // TODO Do something with the thumbnail
                }
            }
            else
            {
                var outputFileUri = data.GetParcelableArrayExtra("outputFileuri");
                // TODO Do something with the full image stored
                // in outputFileUri
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文