android 照个图 要开线程 不然永远拍的图都是那么的小

http://docs.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

Main.axml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/openCamera" />
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="300.0dp"
        android:id="@+id/imageView1"
        android:adjustViewBounds="true" />
</LinearLayout>


 

Strings.xml

<string name="openCamera">Open Camera</string>

 

定义几个全局变量

Java.IO.File _file;

Java.IO.File _dir;

ImageView _imageView;

入口处:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);

    if (IsThereAnAppToTakePictures())
    {
        CreateDirectoryForPictures();

        Button button = FindViewById<Button>(Resource.Id.myButton);
        _imageView = FindViewById<ImageView>(Resource.Id.imageView1);

        button.Click += TakeAPicture;
    }
}

 

两个铺助方法 这个很重要 没有它就不成功了

private bool IsThereAnAppToTakePictures()
{
    Intent intent = new Intent(MediaStore.ActionImageCapture);
    IList<ResolveInfo> availableActivities = PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
    return availableActivities != null && availableActivities.Count > 0;
}

private void CreateDirectoryForPictures()
{
    _dir = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "CameraAppDemo");
    if (!_dir.Exists())
    {
        _dir.Mkdirs();
    }
}




事件开始

private void TakeAPicture(object sender, EventArgs eventArgs)
{
    Intent intent = new Intent(MediaStore.ActionImageCapture);

    _file = new File(_dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));

    intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));

    StartActivityForResult(intent, 0);
}

重载涵数

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    // make it available in the gallery
    Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
    Uri contentUri = Uri.FromFile(_file);
    mediaScanIntent.SetData(contentUri);
    SendBroadcast(mediaScanIntent);

    // display in ImageView. We will resize the bitmap to fit the display
    // Loading the full sized image will consume to much memory 
    // and cause the application to crash.
    int height = _imageView.Height;
    int width = Resources.DisplayMetrics.WidthPixels;
    using (Bitmap bitmap = _file.Path.LoadAndResizeBitmap(width, height))
    {
        _imageView.SetImageBitmap(bitmap);
    }
}


缩略图

public static class BitmapHelpers
{
    public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
    {
        // First we get the the dimensions of the file on disk
        BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
        BitmapFactory.DecodeFile(fileName, options);

        // Next we calculate the ratio that we need to resize the image by
        // in order to fit the requested dimensions.
        int outHeight = options.OutHeight;
        int outWidth = options.OutWidth;
        int inSampleSize = 1;

        if (outHeight > height || outWidth > width)
        {
            inSampleSize = outWidth > outHeight
                               ? outHeight / height
                               : outWidth / width;
        }

        // Now we will load the image and have BitmapFactory resize it for us.
        options.InSampleSize = inSampleSize;
        options.InJustDecodeBounds = false;
        Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);

        return resizedBitmap;
    }
}




 

android 照个图 要开线程 不然永远拍的图都是那么的小,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。