POS-45 Dodanie możliwości robienia zdjęć

This commit is contained in:
Wojciech Przybyła 2021-01-17 23:50:57 +01:00
parent 966edab483
commit 8ce472a931
5 changed files with 637 additions and 528 deletions

View File

@ -12,15 +12,28 @@ using System.Collections.Generic;
using System.Security; using System.Security;
using System.Net; using System.Net;
using System.Linq; using System.Linq;
using System.IO;
using Newtonsoft.Json; using Newtonsoft.Json;
using Klient.Droid.Modules; using Klient.Droid.Modules;
using Android.Provider;
using Android.Content;
using Android.Graphics;
using Com.Xamarin.Formsviewgroup;
using Android.Icu.Text;
using System.IO;
using Android.Media;
using System.Threading.Tasks;
using Java.IO;
using System.Net.Http.Headers;
namespace Klient.Droid namespace Klient.Droid
{ {
[Activity(Label = "Klient", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] [Activity(Label = "Klient", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{ {
Bitmap bitmap;
ByteArrayContent fileContent;
public static int PickImageId = 1000; public static int PickImageId = 1000;
private static readonly HttpClient client = new HttpClient(); private static readonly HttpClient client = new HttpClient();
public static Android.Content.Intent photo; public static Android.Content.Intent photo;
@ -36,8 +49,10 @@ namespace Klient.Droid
base.OnCreate(savedInstanceState); base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.SetVmPolicy(builder.Build());
LoadApplication(new App()); LoadApplication(new App());
SetContentView(Resource.Layout.ekranStartowy); SetContentView(Resource.Layout.ekranPoLogowaniu);
} }
[Java.Interop.Export("SignIn")] [Java.Interop.Export("SignIn")]
@ -202,6 +217,18 @@ namespace Klient.Droid
StartActivityForResult(Android.Content.Intent.CreateChooser(Intent, "Select Picture"), PickImageId); StartActivityForResult(Android.Content.Intent.CreateChooser(Intent, "Select Picture"), PickImageId);
} }
[Java.Interop.Export("LoadFromCamera")]
public void LoadFromCamera(View v)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
//Android.Net.Uri pictureUri = Android.Net.Uri.FromFile(new File(GetExternalFilesDir(Android.OS.Environment.DirectoryDcim), Guid.NewGuid().ToString().Replace(".", "")+".jpg"));
//intent.PutExtra(MediaStore.ExtraOutput, pictureUri);
StartActivityForResult(intent, 0);
}
[Java.Interop.Export("GoToStart")] [Java.Interop.Export("GoToStart")]
public void GoToStart(View v) public void GoToStart(View v)
{ {
@ -216,20 +243,94 @@ namespace Klient.Droid
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data) protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{ {
SetContentView(Resource.Layout.ekranPoZdjeciu); SetContentView(Resource.Layout.ekranPoZdjeciu);
Android.Net.Uri uri = data.Data; FindViewById<TextView>(Resource.Id.ErrorPhoto).Text = "";
photo = data; if (requestCode == 0)
FindViewById<ImageView>(Resource.Id.Preview).SetImageURI(uri); {
photo = data;
base.OnActivityResult(requestCode, resultCode, data);
if(data != null) bitmap = (Bitmap)data.Extras.Get("data");
byte[] bitmapData;
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 60, stream);
bitmapData = stream.ToArray();
fileContent = new ByteArrayContent(bitmapData);
FindViewById<ImageView>(Resource.Id.Preview).SetImageBitmap(bitmap);
}
else if(requestCode == PickImageId)
{
Android.Net.Uri uri = data.Data;
photo = data;
FindViewById<ImageView>(Resource.Id.Preview).SetImageURI(uri);
}
else if(data == null)
{
SetContentView(Resource.Layout.ekranPoLogowaniu);
}
} }
[Java.Interop.Export("SendPhoto")] [Java.Interop.Export("SendPhoto")]
async public void UploadPhoto(View v) async public void UploadPhoto(View v)
{ {
if(photo.Data == null)
{
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "image",
FileName = Guid.NewGuid().ToString()
};
string boundary = "---8d0f01e6b3b5dafaaadaad";
MultipartFormDataContent formDataContent = new MultipartFormDataContent(boundary);
formDataContent.Add(fileContent);
var response = await client.PostAsync("http://10.0.2.2:5001/api/Image/Process", formDataContent);
if (response.StatusCode == HttpStatusCode.OK)
{
var jsonString = await response.Content.ReadAsStringAsync();
dynamic jsonObject = JsonConvert.DeserializeObject(jsonString);
FindViewById<TextView>(Resource.Id.ErrorPhoto).Text = "Przesłano zdjęcie!";
SetContentView(Resource.Layout.ekranZLinkami);
var second_response = await client.GetAsync("http://10.0.2.2:5001/api/Search", jsonObject.response);
if (second_response.StatusCode == HttpStatusCode.OK)
{
var second_jsonString = await response.Content.ReadAsStringAsync();
dynamic second_jsonObject = JsonConvert.DeserializeObject(second_jsonString);
List<SearchResult> fullLink = JsonConvert.DeserializeObject<List<SearchResult>>(second_jsonObject);
int end = 10;
if (fullLink.Count < 10) end = fullLink.Count;
for (int i = 0; i < end; i++)
{
FindViewById<TextView>(Resource.Id.links).Text = FindViewById<TextView>(Resource.Id.links).Text + fullLink[i].Title + ": " + fullLink[i].Link + "\n\n";
}
}
else
{
FindViewById<TextView>(Resource.Id.links).Text = "Coś poszło nie tak!";
}
}
else
{
string msg = await response.Content.ReadAsStringAsync();
FindViewById<TextView>(Resource.Id.ErrorPhoto).Text = msg;
}
}
using(var imageStream = ContentResolver.OpenInputStream(photo.Data)) using(var imageStream = ContentResolver.OpenInputStream(photo.Data))
using(var stramContent = new StreamContent(imageStream)) using(var stramContent = new StreamContent(imageStream))
using(var byteArrayContent = new ByteArrayContent(await stramContent.ReadAsByteArrayAsync())) using(var byteArrayContent = new ByteArrayContent(await stramContent.ReadAsByteArrayAsync()))
using(var formDataContent = new MultipartFormDataContent()) using(var formDataContent = new MultipartFormDataContent())
{ {
formDataContent.Add(byteArrayContent, "image", Guid.NewGuid() + ".jpg"); formDataContent.Add(byteArrayContent, "image", Guid.NewGuid() + ".jpg");
foreach (var content in formDataContent) foreach (var content in formDataContent)
{ {
@ -270,7 +371,7 @@ namespace Klient.Droid
string msg = await response.Content.ReadAsStringAsync(); string msg = await response.Content.ReadAsStringAsync();
FindViewById<TextView>(Resource.Id.ErrorPhoto).Text = msg; FindViewById<TextView>(Resource.Id.ErrorPhoto).Text = msg;
} }
} }
} }
[Java.Interop.Export("GoToProfil")] [Java.Interop.Export("GoToProfil")]

View File

@ -36,6 +36,7 @@
<BundleAssemblies>false</BundleAssemblies> <BundleAssemblies>false</BundleAssemblies>
<MandroidI18n /> <MandroidI18n />
<Debugger>Xamarin</Debugger> <Debugger>Xamarin</Debugger>
<AndroidSupportedAbis />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@ -177,6 +178,9 @@
<ItemGroup> <ItemGroup>
<AndroidResource Include="Resources\drawable\Obraz4.png" /> <AndroidResource Include="Resources\drawable\Obraz4.png" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\xml\file_paths.xml" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<ProjectExtensions> <ProjectExtensions>
<VisualStudio> <VisualStudio>

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?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="com.companyname.Klient.Android" android:installLocation="preferExternal"> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.Klient.Android" android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" /> <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="Klient.Android" android:usesCleartextTraffic="true" android:theme="@style/Base.Theme.AppCompat.Light.DarkActionBar" /> <application android:label="Klient.Android" android:usesCleartextTraffic="true" android:theme="@style/Base.Theme.AppCompat.Light.DarkActionBar" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest> </manifest>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="my_images"
path="path" />
</paths>