battle programmers alliance

battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Search
 
 

Display results as :
 
Rechercher Advanced Search
Latest topics
» xamarin c# manual image app walkthrough
Sat Aug 12, 2017 2:08 pm by Moti Barski

» win10 shortcut keys
Wed Aug 09, 2017 2:40 am by Moti Barski

» vb.net region
Sat Aug 05, 2017 12:25 am by Moti Barski

» visual studio 2017 xamarin c# hello world walkthrough
Sat Jul 29, 2017 11:34 am by Admin

» gear s3 frontier s voice command list
Sat Jul 22, 2017 7:07 pm by Moti Barski

» how to use magnet links
Wed Jun 28, 2017 3:41 am by kurosen

» evolution of code
Mon Jun 12, 2017 12:08 am by kurosen

» chicken bot
Sat Apr 29, 2017 11:09 am by Moti Barski

» vb.net convert to binary
Thu Apr 27, 2017 7:48 am by Moti Barski

August 2017
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
2728293031  
CalendarCalendar
Social bookmarking
Social bookmarking Digg  Social bookmarking Delicious  Social bookmarking Reddit  Social bookmarking Stumbleupon  Social bookmarking Slashdot  Social bookmarking Yahoo  Social bookmarking Google  Social bookmarking Blinklist  Social bookmarking Blogmarks  Social bookmarking Technorati  

Bookmark and share the address of battle programmers alliance on your social bookmarking website
Share | 
 

 xamarin c# manual image app walkthrough

View previous topic View next topic Go down 
AuthorMessage
Moti Barski

avatar

Posts : 79
Join date : 2011-08-02

PostSubject: xamarin c# manual image app walkthrough   Sat Aug 12, 2017 2:08 pm

take a picture
new project, c#, android, single view app
Open Solution Explorer-> Project Name->Resources->Layout->Main.axml. Click Open Design View
solution explorer, resources, main.axml code :
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageView"
        android:layout_weight="9"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" />
    <Button
        android:layout_weight="1"
        android:id="@+id/btnCamera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Open Camera" />
</LinearLayout>
Go to Solution Explorer-> Project Name. Right click to Add->Class, followed by opening new Dialog box. This dialog box is required to select the class and give name for BitmapHelpers.cs.
Open Solution Explorer-> Project Name->Resources-> BitmapHelpers.cs. This class is reducing the image size and recycling the memory. It will be used to calculate an image ratio.
code
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using Android.Graphics.Drawables;
namespace Camera
{
    public static class BitmapHelpers
    {
        /// This method will recyle the memory help by a bitmap in an ImageView  
        public static void RecycleBitmap(this ImageView imageView)
        {
            if (imageView == null)
            {
                return;
            }
            Drawable toRecycle = imageView.Drawable;
            if (toRecycle != null)
            {
                ((BitmapDrawable)toRecycle).Bitmap.Recycle();
            }
        }
        /// Load the image from the device, and resize it to the specified dimensions.  
        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
            {
                InPurgeable = true,
                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;
        }
    }
}

Open Solution Explorer-> Project Name->Resources-> MainActicity.cs. Add code, given below-

Code:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Telephony;
using Android.Provider;
using Java.IO;
using Android.Graphics;
using Android.Content.PM;
using Uri = Android.Net.Uri;
using System.Collections.Generic;
using Camera;

namespace TheInternship
{
    [Activity(Label = "TheInternship", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        Button BtnTakeImg;
        ImageView ImgView;
        public static File _file;
        public static File _dir;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource  
            SetContentView(Resource.Layout.Main);
            if (IsThereAnAppToTakePictures())
            {
                CreateDirectoryForPictures();
                BtnTakeImg = FindViewById<Button>(Resource.Id.btnCamera);
                ImgView = FindViewById<ImageView>(Resource.Id.imageView);
                BtnTakeImg.Click += TakeAPicture;
            }
        }
        private void CreateDirectoryForPictures()
        {
            _dir = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "C#Corner");
            if (!_dir.Exists())
            {
                _dir.Mkdirs();
            }
        }
        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 TakeAPicture(object sender, EventArgs eventArgs)
        {


            Intent intent = new Intent(MediaStore.ActionImageCapture);
            _file = new File(_dir, string.Format("Image_{0}.jpg", Guid.NewGuid()));
            intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));
            StartActivityForResult(intent, 102);
        }
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if (requestCode == 102 && resultCode == Result.Ok)
            {
                // make it available in the gallery  
                Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                Uri contentUri = Uri.FromFile(_file);
                mediaScanIntent.SetData(contentUri);
                SendBroadcast(mediaScanIntent);
                //Converstion Image Size  
                int height = ImgView.Height;
                int width = Resources.DisplayMetrics.WidthPixels;
                using (Bitmap bitmap = _file.Path.LoadAndResizeBitmap(width, height))
                {
                    //View ImageView  
                    ImgView.RecycleBitmap();
                    ImgView.SetImageBitmap(bitmap);
                    //Upload Image in Database  
                }
            }
        }
    }

}
run app
Back to top Go down
View user profile
 
xamarin c# manual image app walkthrough
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» How do I make the login popup show an Image, instead of it's bland colors
» Roster image
» The header image keeps going invisible.
» Tiling image behind banner on PhpBB?
» How do you copy image URLs?

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: battle programming-
Jump to: