added cropping to send avatar (vocaluxe app)

This commit is contained in:
Alexander Eckart committed 2013-06-09 16:48:07 +02:00
1 parent c83ade5e6e
commit 88ab44608b
2 files changed
+101 -63

No files matched your search

+91 -63
View File
@@ -19,6 +19,9 @@ namespace org.vocaluxe.app
[Activity (Label = "Vocaluxe App", MainLauncher = true, ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation)] [Activity (Label = "Vocaluxe App", MainLauncher = true, ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation)]
public class Activity1 : Activity public class Activity1 : Activity
{ {
private readonly int REQUEST_TAKE_PICTURE = 1;
private readonly int REQUEST_PICK_PICTURE = 2;
CClient client = new CClient(); CClient client = new CClient();
CDiscover discover; CDiscover discover;
Button bConnect; Button bConnect;
@@ -28,10 +31,12 @@ namespace org.vocaluxe.app
TextView tIP; TextView tIP;
TextView tPassword; TextView tPassword;
bool loggedIn; ProgressDialog pDialog;
byte[] fileBytes; byte[] fileBytes;
Java.IO.File file; Java.IO.File file;
Java.IO.File file2;
Java.IO.File dir; Java.IO.File dir;
System.IO.FileStream fs; System.IO.FileStream fs;
@@ -40,11 +45,10 @@ namespace org.vocaluxe.app
protected override void OnCreate (Bundle bundle) protected override void OnCreate (Bundle bundle)
{ {
base.OnCreate (bundle); base.OnCreate (bundle);
loggedIn = false;
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main); SetContentView (Resource.Layout.Main);
pDialog = new ProgressDialog (this);
pDialog.SetCancelable (false);
bConnect = FindViewById<Button> (Resource.Id.btConnect); bConnect = FindViewById<Button> (Resource.Id.btConnect);
bConnect.Click += delegate { Connect(); }; bConnect.Click += delegate { Connect(); };
@@ -75,19 +79,55 @@ namespace org.vocaluxe.app
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{ {
base.OnActivityResult(requestCode, resultCode, data); //base.OnActivityResult(requestCode, resultCode, data);
if (resultCode != Result.Ok)
// make it available in the gallery return;
Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
Android.Net.Uri contentUri = Android.Net.Uri.FromFile(file); if (requestCode == REQUEST_TAKE_PICTURE)
mediaScanIntent.SetData(contentUri); {
SendBroadcast(mediaScanIntent); // make it available in the gallery
System.IO.FileInfo fi = new System.IO.FileInfo(file.Path); //Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
fs = new System.IO.FileStream(file.Path, System.IO.FileMode.Open); Android.Net.Uri contentUri = Android.Net.Uri.FromFile(file);
fileBytes = new byte[fi.Length]; //mediaScanIntent.SetData(contentUri);
fs.BeginRead (fileBytes, 0, (int)fi.Length, new AsyncCallback(OnFileRead), null); //SendBroadcast(mediaScanIntent);
Java.IO.File dir2 = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), Resources.GetString(Resource.String.app_name));
if (!dir.Exists())
{
dir.Mkdirs();
}
file2 = new Java.IO.File(dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
Intent picker = new Intent ("com.android.camera.action.CROP");
//Intent picker = new Intent (Intent.ActionGetContent, contentUri);
picker.SetDataAndType (contentUri, "image/*");
picker.PutExtra ("crop", "true");
picker.PutExtra ("aspectX", 1);
picker.PutExtra ("aspectY", 1);
picker.PutExtra ("outputX", 512);
picker.PutExtra ("outputY", 512);
picker.PutExtra ("return-data", false);
//picker.PutExtra ("outputFormat", Bitmap.CompressFormat.Jpeg.ToString ());
picker.PutExtra ("noFaceDetection", true);
picker.PutExtra (MediaStore.ExtraOutput, Android.Net.Uri.FromFile(file2));
StartActivityForResult (picker, REQUEST_PICK_PICTURE);
}
if (requestCode == REQUEST_PICK_PICTURE)
{
if (data.Extras != null)
{
System.IO.FileInfo fi = new System.IO.FileInfo(file2.Path);
fs = new System.IO.FileStream(file2.Path, System.IO.FileMode.Open);
fileBytes = new byte[fi.Length];
fs.BeginRead (fileBytes, 0, (int)fi.Length, new AsyncCallback(OnFileRead), null);
fi = null;
}
}
} }
private void OnFileRead(IAsyncResult ar) private void OnFileRead(IAsyncResult ar)
{ {
if (fileBytes == null) if (fileBytes == null)
@@ -96,6 +136,12 @@ namespace org.vocaluxe.app
fs.Flush(); fs.Flush();
fs.Close(); fs.Close();
if (file != null)
file.Delete ();
if (file2 != null)
file2.Delete ();
if (sendProfile) if (sendProfile)
{ {
sendProfile = false; sendProfile = false;
@@ -103,7 +149,6 @@ namespace org.vocaluxe.app
if (tv.Text == String.Empty) if (tv.Text == String.Empty)
return; return;
client.SendMessage (CCommands.CreateCommandSendProfile(fileBytes, tv.Text, 0), OnResponse); client.SendMessage (CCommands.CreateCommandSendProfile(fileBytes, tv.Text, 0), OnResponse);
} }
else else
@@ -119,6 +164,18 @@ namespace org.vocaluxe.app
private void SendAvatar(object sender, EventArgs eventArgs) private void SendAvatar(object sender, EventArgs eventArgs)
{ {
_TakePicture (false);
}
private void SendProfile(object sender, EventArgs eventArgs)
{
_TakePicture (true);
}
private void _TakePicture(bool SendProfile)
{
sendProfile = SendProfile;
if (!IsThereAnAppToTakePictures()) if (!IsThereAnAppToTakePictures())
return; return;
@@ -131,36 +188,31 @@ namespace org.vocaluxe.app
file = new Java.IO.File(dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid())); file = new Java.IO.File(dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(file)); intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(file));
StartActivityForResult(intent, 0); StartActivityForResult(intent, REQUEST_TAKE_PICTURE);
}
private void SendProfile(object sender, EventArgs eventArgs)
{
sendProfile = true;
SendAvatar(sender, eventArgs);
} }
private void Connect() private void Connect()
{ {
if (!client.Connected) if (!client.Connected)
{
pDialog.SetMessage ("Connecting for Server...");
pDialog.Show ();
client.Connect(tIP.Text, 3000, tPassword.Text, OnConnectionChanged); client.Connect(tIP.Text, 3000, tPassword.Text, OnConnectionChanged);
}
else else
{
pDialog.Hide ();
client.Disconnect(); client.Disconnect();
}
} }
private void Discover() private void Discover()
{ {
pDialog.SetMessage ("Searching for Server...");
pDialog.Show ();
discover.Discover (_OnDiscovered); discover.Discover (_OnDiscovered);
} }
private void Login()
{
if (!loggedIn)
client.SendMessage (CCommands.CreateCommandLogin(tPassword.Text), OnResponse);
else
client.Disconnect();
}
private void OnConnectionChanged(bool Connected) private void OnConnectionChanged(bool Connected)
{ {
RunOnUiThread (() => UpdateConnectionStatus(Connected)); RunOnUiThread (() => UpdateConnectionStatus(Connected));
@@ -194,13 +246,16 @@ namespace org.vocaluxe.app
{ {
RunOnUiThread (() => RunOnUiThread (() =>
{ {
if (Address != CDiscover.sTimeout) if (Address != CDiscover.sTimeout && Address != CDiscover.sFinished)
{ {
tIP.Text = Address; tIP.Text = Address;
Connect (); Connect ();
} }
else else if (Address == CDiscover.sTimeout)
{
pDialog.Hide ();
Toast.MakeText(this, "Cant't find any Vocaluxe Server", ToastLength.Short).Show(); Toast.MakeText(this, "Cant't find any Vocaluxe Server", ToastLength.Short).Show();
}
}); });
} }
@@ -208,6 +263,7 @@ namespace org.vocaluxe.app
{ {
if (Connected) if (Connected)
{ {
pDialog.Hide ();
bConnect.Text = Resources.GetString(Resource.String.button_disconnect); bConnect.Text = Resources.GetString(Resource.String.button_disconnect);
Toast.MakeText(this, Resource.String.message_connected, ToastLength.Short).Show(); Toast.MakeText(this, Resource.String.message_connected, ToastLength.Short).Show();
bSendAvatar.Enabled = true; bSendAvatar.Enabled = true;
@@ -215,6 +271,7 @@ namespace org.vocaluxe.app
} }
else else
{ {
pDialog.Hide ();
client.Disconnect(); client.Disconnect();
bConnect.Text = Resources.GetString(Resource.String.button_connect); bConnect.Text = Resources.GetString(Resource.String.button_connect);
bSendAvatar.Enabled = false; bSendAvatar.Enabled = false;
@@ -223,35 +280,6 @@ namespace org.vocaluxe.app
} }
} }
} }
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;
}
}
} }
@@ -9,53 +9,63 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/textView1" android:id="@+id/textView1"
android:layout_margin="3dp"
android:linksClickable="false" /> android:linksClickable="false" />
<EditText <EditText
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/tbIP" android:id="@+id/tbIP"
android:layout_margin="3dp"
android:text="@string/ip_address" /> android:text="@string/ip_address" />
<TextView <TextView
android:text="@string/text_password" android:text="@string/text_password"
android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="3dp"
android:id="@+id/textView2" /> android:id="@+id/textView2" />
<EditText <EditText
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/tbPassword" android:id="@+id/tbPassword"
android:layout_margin="3dp"
android:text="@string/password" /> android:text="@string/password" />
<Button <Button
android:text="@string/button_connect" android:text="@string/button_connect"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="3dp"
android:id="@+id/btConnect" /> android:id="@+id/btConnect" />
<Button <Button
android:text="Discover" android:text="Discover"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="3dp"
android:id="@+id/btDiscover" /> android:id="@+id/btDiscover" />
<Button <Button
android:text="@string/button_send_avatar" android:text="@string/button_send_avatar"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/btSendAvatar" android:id="@+id/btSendAvatar"
android:layout_margin="3dp"
android:enabled="false" /> android:enabled="false" />
<TextView <TextView
android:text="@string/text_player_name" android:text="@string/text_player_name"
android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="3dp"
android:id="@+id/textView3" /> android:id="@+id/textView3" />
<EditText <EditText
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="3dp"
android:id="@+id/tbPlayerName" /> android:id="@+id/tbPlayerName" />
<Button <Button
android:text="@string/button_send_profile" android:text="@string/button_send_profile"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/btSendProfile" android:id="@+id/btSendProfile"
android:layout_margin="3dp"
android:enabled="false" /> android:enabled="false" />
</LinearLayout> </LinearLayout>