take profile photos by android phone (still very buggy)

This commit is contained in:
Alexander Eckart committed 2013-05-06 22:08:59 +02:00
1 parent da843b4f06
commit 6d008cdc06
17 files changed
+339 -48

No files matched your search

@@ -15,6 +15,7 @@
<AndroidApplication>True</AndroidApplication>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AssemblyName>AndroidRemote</AssemblyName>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -78,7 +79,7 @@
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" />
<None Include="Properties\AndroidManifest.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\Main.axml" />
@@ -88,5 +89,6 @@
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
<Folder Include="ClientServerLib\" />
<Folder Include="Assets\" />
</ItemGroup>
</Project>
+95 -1
View File
@@ -1,11 +1,15 @@
using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Provider;
using ClientServerLib;
using Vocaluxe.Base.Server;
@@ -20,7 +24,14 @@ namespace AndroidRemote
Button bLogin;
Button bUp;
Button bDown;
Button bSendAvatar;
bool loggedIn;
byte[] fileBytes;
ImageView imageView;
Java.IO.File file;
Java.IO.File dir;
System.IO.FileStream fs;
protected override void OnCreate (Bundle bundle)
{
@@ -46,12 +57,66 @@ namespace AndroidRemote
bDown.Click += delegate {
client.SendMessage (CCommands.CreateCommandWithoutParams (CCommands.CommandSendKeyDown), OnResponse);
};
imageView = FindViewById<ImageView>(Resource.Id.imageView1);
bSendAvatar = FindViewById<Button> (Resource.Id.btSendAvatar);
bSendAvatar.Click += SendAvatar;
}
protected override void OnStop ()
{
base.OnStop();
client.Close();
//client.Close();
}
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);
Android.Net.Uri contentUri = Android.Net.Uri.FromFile(file);
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent);
System.IO.FileInfo fi = new System.IO.FileInfo(file.Path);
fs = new System.IO.FileStream(file.Path, System.IO.FileMode.Open);
fileBytes = new byte[fi.Length];
fs.BeginRead (fileBytes, 0, (int)fi.Length, new AsyncCallback(OnFileRead), null);
}
private void OnFileRead(IAsyncResult ar)
{
if (fileBytes == null)
return;
fs.Flush();
fs.Close();
client.SendMessage (CCommands.CreateCommandSendAvatarPictureJpg(fileBytes), OnResponse);
}
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 SendAvatar(object sender, EventArgs eventArgs)
{
if (!IsThereAnAppToTakePictures())
return;
Intent intent = new Intent(MediaStore.ActionImageCapture);
dir = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "AndroidRemote");
if (dir.Exists())
{
dir.Mkdirs();
}
file = new Java.IO.File(dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(file));
StartActivityForResult(intent, 0);
}
private void Connect()
@@ -138,6 +203,35 @@ namespace AndroidRemote
Toast.MakeText(this, "Not Logged In", ToastLength.Short).Show();
}
}
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;
}
}
}
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="AndroidRemote.AndroidRemote">
<uses-sdk />
<application android:label="AndroidRemote" android:icon="@drawable/icon"></application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
+22 -13
View File
@@ -72,9 +72,15 @@ namespace AndroidRemote
// aapt resource value: 0x7f060005
public const int btLogin = 2131099653;
// aapt resource value: 0x7f060008
public const int btSendAvatar = 2131099656;
// aapt resource value: 0x7f060006
public const int btUp = 2131099654;
// aapt resource value: 0x7f060009
public const int imageView1 = 2131099657;
// aapt resource value: 0x7f060001
public const int tbIP = 2131099649;
@@ -137,32 +143,35 @@ namespace AndroidRemote
// aapt resource value: 0x7f040004
public const int button_logout = 2130968580;
// aapt resource value: 0x7f040007
public const int button_send_avatar = 2130968583;
// aapt resource value: 0x7f040005
public const int button_up = 2130968581;
// aapt resource value: 0x7f04000d
public const int ip_address = 2130968589;
// aapt resource value: 0x7f040007
public const int message_connected = 2130968583;
// aapt resource value: 0x7f04000e
public const int ip_address = 2130968590;
// aapt resource value: 0x7f040008
public const int message_disconnected = 2130968584;
public const int message_connected = 2130968584;
// aapt resource value: 0x7f040009
public const int message_logged_in = 2130968585;
public const int message_disconnected = 2130968585;
// aapt resource value: 0x7f04000a
public const int message_logged_out = 2130968586;
// aapt resource value: 0x7f04000e
public const int password = 2130968590;
public const int message_logged_in = 2130968586;
// aapt resource value: 0x7f04000b
public const int text_ip_address = 2130968587;
public const int message_logged_out = 2130968587;
// aapt resource value: 0x7f04000f
public const int password = 2130968591;
// aapt resource value: 0x7f04000c
public const int text_password = 2130968588;
public const int text_ip_address = 2130968588;
// aapt resource value: 0x7f04000d
public const int text_password = 2130968589;
static String()
{
@@ -49,4 +49,15 @@
android:layout_height="wrap_content"
android:id="@+id/btDown"
android:enabled="false" />
<Button
android:text="@string/button_send_avatar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btSendAvatar"
android:enabled="true" />
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</LinearLayout>
@@ -7,6 +7,7 @@
<string name="button_logout">Logout</string>
<string name="button_up">Up</string>
<string name="button_down">Down</string>
<string name="button_send_avatar">Send Avatar Picture</string>
<string name="message_connected">Connected</string>
<string name="message_disconnected">Disconnected</string>
<string name="message_logged_in">Logged in</string>
+27 -8
View File
@@ -24,6 +24,7 @@ namespace ClientServerLib
private CConnection connection;
private List<SRequest> requests;
private List<SRequest> responses;
private Object mutexRequests = new Object();
private OnConnectionChanged onConnectionChanged;
private OnSend onSend;
private OnReceived onReceived;
@@ -70,7 +71,7 @@ namespace ClientServerLib
{
doConnect = false;
lock (requests)
lock (mutexRequests)
{
requests.Clear();
}
@@ -96,7 +97,7 @@ namespace ClientServerLib
req.Callback = Callback;
req.Response = null;
lock (requests)
lock (mutexRequests)
{
requests.Add(req);
}
@@ -195,15 +196,23 @@ namespace ClientServerLib
}
else
{
lock (requests)
try
{
while (requests.Count > 0)
{
SRequest res = HandleRequest(requests[0]);
responses.Add(res);
requests.RemoveAt(0);
lock (responses)
{
responses.Add(res);
}
lock (mutexRequests)
{
requests.RemoveAt(0);
}
}
}
catch { };
lock (responses)
{
@@ -236,7 +245,16 @@ namespace ClientServerLib
RaiseOnSend(request.Command);
byte[] encrypted = connection.Encrypt(request.Command);
clientStream.Write(encrypted, 0, encrypted.Length);
int sendBytes = 0;
while(sendBytes < encrypted.Length)
{
int bytesToSend = encrypted.Length - sendBytes;
if (bytesToSend > bufferLength)
bytesToSend = bufferLength;
clientStream.Write(encrypted, sendBytes, bytesToSend);
sendBytes += bytesToSend;
}
clientStream.Flush();
byte[] data = new byte[bufferLength];
@@ -257,8 +275,9 @@ namespace ClientServerLib
int messageLength = BitConverter.ToInt32(data, 0);
while (messageLength > bytesRead)
{
Array.Resize<byte>(ref data, data.Length + bufferLength);
bytesRead += clientStream.Read(data, data.Length - bufferLength, bufferLength);
if (data.Length < messageLength + bufferLength)
Array.Resize<byte>(ref data, messageLength + bufferLength);
bytesRead += clientStream.Read(data, bytesRead, bufferLength);
}
encrypted = new byte[bytesRead];
+20 -10
View File
@@ -32,6 +32,7 @@ namespace ClientServerLib
public CConnection(TcpClient client, int ConnectionID)
{
TcpClient = client;
client.NoDelay = true;
keySet = false;
connectionID = ConnectionID;
encoder = new UTF8Encoding();
@@ -125,17 +126,26 @@ namespace ClientServerLib
byte[] encrypted = new byte[messageLength - 24];
Array.Copy(Data, 24, encrypted, 0, encrypted.Length);
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
try
{
cryptoStream.Write(encrypted, 0, encrypted.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
byte[] decrypted = memoryStream.ToArray();
byte[] message = new byte[dataLength];
Array.Copy(decrypted, message, dataLength);
return message;
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(encrypted, 0, encrypted.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
byte[] decrypted = memoryStream.ToArray();
byte[] message = new byte[dataLength];
Array.Copy(decrypted, message, dataLength);
return message;
}
}
catch
{
return null;
}
}
}
+3 -2
View File
@@ -146,8 +146,9 @@ namespace ClientServerLib
int messageLength = BitConverter.ToInt32(data, 0);
while (messageLength > bytesRead)
{
Array.Resize<byte>(ref data, data.Length + bufferLength);
bytesRead += clientStream.Read(data, data.Length - bufferLength, bufferLength);
if (data.Length < messageLength + bufferLength)
Array.Resize<byte>(ref data, messageLength + bufferLength);
bytesRead += clientStream.Read(data, bytesRead, bufferLength);
}
byte[] message = new byte[bytesRead];
@@ -12,9 +12,9 @@ namespace Vocaluxe.Base.Server
public static class CCommands
{
private static UTF8Encoding encoder = new UTF8Encoding();
public static SHA256Managed SHA256 = new SHA256Managed();
#region Commands
public const int ResponseOK = 1;
public const int ResponseNOK = 2;
@@ -39,6 +39,10 @@ namespace Vocaluxe.Base.Server
public const int ComamndSendMouseMBUpEvent = 241;
public const int CommandSendMouseWheelEvent = 250;
public const int CommandSendAvatarPicture = 500;
public const int CommandSendAvatarPictureJpg = 501;
#endregion Commands
#region General
public static byte[] CreateCommandWithoutParams(int Command)
{
@@ -65,7 +69,36 @@ namespace Vocaluxe.Base.Server
#endregion Keyboard
#region Avatar
public static byte[] CreateCommandSendAvatarPicture(int Width, int Height, byte[] data)
{
SAvatarPicture ap = new SAvatarPicture();
ap.Height = Height;
ap.Width = Width;
ap.data = new byte[data.Length];
Array.Copy(data, ap.data, data.Length);
return Serialize<SAvatarPicture>(CommandSendAvatarPicture, ap);
}
public static bool DecodeCommandSendAvatarPicture(byte[] Message, out SAvatarPicture AvatarPicture)
{
return TryDeserialize<SAvatarPicture>(Message, out AvatarPicture);
}
public static byte[] CreateCommandSendAvatarPictureJpg(byte[] data)
{
SAvatarPicture ap = new SAvatarPicture();
ap.Height = 0;
ap.Width = 0;
ap.data = new byte[data.Length];
Array.Copy(data, ap.data, data.Length);
return Serialize<SAvatarPicture>(CommandSendAvatarPictureJpg, ap);
}
#endregion Avatar
#region Serializing
private static byte[] Serialize<T>(int Command, T obj)
{
byte[] command = BitConverter.GetBytes(Command);
@@ -108,5 +141,6 @@ namespace Vocaluxe.Base.Server
}
}
}
#endregion Serializing
}
}
@@ -6,4 +6,11 @@ namespace Vocaluxe.Base.Server
{
public byte[] SHA256;
}
public struct SAvatarPicture
{
public int Width;
public int Height;
public byte[] data;
}
}
+14
View File
@@ -47,6 +47,7 @@
this.btRight = new System.Windows.Forms.Button();
this.btUp = new System.Windows.Forms.Button();
this.btDown = new System.Windows.Forms.Button();
this.btSendAvatar = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
@@ -227,11 +228,23 @@
this.btDown.UseVisualStyleBackColor = true;
this.btDown.Click += new System.EventHandler(this.btDown_Click);
//
// btSendAvatar
//
this.btSendAvatar.Enabled = false;
this.btSendAvatar.Location = new System.Drawing.Point(831, 310);
this.btSendAvatar.Name = "btSendAvatar";
this.btSendAvatar.Size = new System.Drawing.Size(165, 23);
this.btSendAvatar.TabIndex = 14;
this.btSendAvatar.Text = "Send Avatar";
this.btSendAvatar.UseVisualStyleBackColor = true;
this.btSendAvatar.Click += new System.EventHandler(this.btSendAvatar_Click);
//
// ClientTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1008, 550);
this.Controls.Add(this.btSendAvatar);
this.Controls.Add(this.btDown);
this.Controls.Add(this.btUp);
this.Controls.Add(this.btRight);
@@ -279,6 +292,7 @@
private System.Windows.Forms.Button btRight;
private System.Windows.Forms.Button btUp;
private System.Windows.Forms.Button btDown;
private System.Windows.Forms.Button btSendAvatar;
}
}
+25 -4
View File
@@ -118,6 +118,7 @@ namespace ClientTest
btDown.Enabled = true;
btLeft.Enabled = true;
btRight.Enabled = true;
btSendAvatar.Enabled = true;
});
}
else
@@ -134,6 +135,7 @@ namespace ClientTest
btDown.Enabled = false;
btLeft.Enabled = false;
btRight.Enabled = false;
btSendAvatar.Enabled = false;
});
}
}
@@ -144,9 +146,12 @@ namespace ClientTest
return;
string text = String.Empty;
foreach (byte b in Message)
int max = Message.Length;
if (max > 1000)
max = 1000;
for (int i = 0; i < max; i++)
{
text += b.ToString() + " ";
text += Message[i].ToString() + " ";
}
this.Invoke((MethodInvoker)delegate
@@ -161,9 +166,12 @@ namespace ClientTest
return;
string text = String.Empty;
foreach (byte b in Message)
int max = Message.Length;
if (max > 1000)
max = 1000;
for (int i = 0; i < max; i++)
{
text += b.ToString() + " ";
text += Message[i].ToString() + " ";
}
this.Invoke((MethodInvoker)delegate
@@ -203,5 +211,18 @@ namespace ClientTest
client.SendMessage(CCommands.CreateCommandWithoutParams(CCommands.CommandSendKeyRight), null);
}
private void btSendAvatar_Click(object sender, EventArgs e)
{
if (!client.Connected || !loggedIn)
return;
byte[] data = new byte[512 * 512 * 4];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte) (i % 255);
}
client.SendMessage(CCommands.CreateCommandSendAvatarPicture(512, 512, data), null);
}
}
}
Binary file not shown.
+1 -2
View File
@@ -58,7 +58,6 @@ namespace Vocaluxe.Base
_Settings.Encoding = Encoding.UTF8;
_Settings.ConformanceLevel = ConformanceLevel.Document;
LoadAvatars();
LoadProfiles();
}
@@ -100,6 +99,7 @@ namespace Vocaluxe.Base
public static void LoadProfiles()
{
LoadAvatars();
_Profiles = new List<SProfile>();
List<string> files = new List<string>();
files.AddRange(CHelper.ListFiles(CSettings.FolderProfiles, "*.xml", true, true));
@@ -128,7 +128,6 @@ namespace Vocaluxe.Base
foreach (string file in files)
{
STexture tex = CDraw.AddTexture(file);
if (tex.Index != -1)
{
SAvatar avatar = new SAvatar {Texture = tex, FileName = Path.GetFileName(file)};
+66 -4
View File
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
@@ -39,11 +43,13 @@ namespace Vocaluxe.Base.Server
private static byte[] RequestHandler(int ConnectionID, byte[] Message)
{
byte[] answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseNOK);
if (Message == null)
return null;
return answer;
if (Message.Length < 4)
return null;
return answer;
bool loggedIn = false;
lock (_Clients)
@@ -58,8 +64,6 @@ namespace Vocaluxe.Base.Server
}
int command = BitConverter.ToInt32(Message, 0);
byte[] answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseNOK); ;
switch (command)
{
case CCommands.CommandLogin:
@@ -107,6 +111,64 @@ namespace Vocaluxe.Base.Server
Controller.AddKeyEvent(new SKeyEvent(ESender.Keyboard, false, false, false, false, Char.MinValue, Keys.Right));
break;
case CCommands.CommandSendAvatarPicture:
SAvatarPicture avatarPicture;
if (CCommands.DecodeCommandSendAvatarPicture(Message, out avatarPicture))
{
Bitmap bmp = null;
bool success = false;
try
{
bmp = new Bitmap(avatarPicture.Width, avatarPicture.Height);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(avatarPicture.data, 0, bmpData.Scan0, avatarPicture.data.Length);
bmp.UnlockBits(bmpData);
const string filename = "snapshot";
int i = 0;
while (File.Exists(Path.Combine(CSettings.FolderProfiles, filename + i + ".png")))
i++;
bmp.Save(Path.Combine(CSettings.FolderProfiles, filename + i + ".png"), ImageFormat.Png);
success = true;
}
finally
{
if (bmp != null)
bmp.Dispose();
}
if (success)
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
}
break;
case CCommands.CommandSendAvatarPictureJpg:
SAvatarPicture avatarPictureJpg;
if (CCommands.DecodeCommandSendAvatarPicture(Message, out avatarPictureJpg))
{
bool success = false;
try
{
string filename = Path.Combine(CSettings.FolderProfiles, "snapshot");
int i = 0;
while (File.Exists(filename + i + ".jpg"))
i++;
FileStream fs = File.Create(filename + i + ".jpg");
fs.Write(avatarPictureJpg.data, 0, avatarPictureJpg.data.Length);
fs.Flush();
fs.Close();
success = true;
}
catch {}
if (success)
answer = CCommands.CreateCommandWithoutParams(CCommands.ResponseOK);
}
break;
default:
break;
}
+1 -1
View File
@@ -86,6 +86,7 @@ namespace Vocaluxe.Screens
_SelectSlides[_SelectSlideDifficulty].SetValues<EGameDifficulty>(0);
_SelectSlides[_SelectSlideGuestProfile].SetValues<EOffOn>(0);
_SelectSlides[_SelectSlideActive].SetValues<EOffOn>(0);
_Statics[_StaticAvatar].Aspect = EAspect.Crop;
}
public override bool HandleInput(SKeyEvent keyEvent)
@@ -315,7 +316,6 @@ namespace Vocaluxe.Screens
if (_Snapshot == null)
CWebcam.GetFrame(ref _WebcamTexture);
_Statics[_StaticAvatar].Aspect = EAspect.Crop;
_Statics[_StaticAvatar].Texture = _WebcamTexture;
}
else