コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
public interface IImageService
{
Stream GetShrinkedStream(string filePath, int width, int height);
}
using System.IO;
using Android.Graphics;
[assembly: Dependency(typeof(ImageService))]
public class ImageService : IImageService
{
public Stream GetShrinkedStream(string filePath, int width, int height)
{
Stream stream = null;
try
{
//画像を圧縮する
Bitmap bitmap = ImageService.GetShrinkedBitmap(filePath, width, height);
//Streamを取得する
stream = new MemoryStream();
bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream); // this is the diff between iOS and Android
stream.Position = 0;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine +
ex.StackTrace);
}
return stream;
}
/// <summary>
/// 縮小したBitmapを取得する
/// </summary>
/// <param name="source">ファイルパス</param>
/// <param name="width">縮小サイズ(幅)</param>
/// <param name="height">縮小サイズ(高さ)</param>
/// <returns></returns>
private static Bitmap GetShrinkedBitmap(string filePath, int width, int height)
{
Bitmap bitmap = null;
try
{
//読み込み用のオプションオブジェクトを生成
//この値をtrueにするとメモリに画像を読み込まず、画像のサイズ情報だけを取得することができます。
var options = new BitmapFactory.Options { InJustDecodeBounds = true };
//画像ファイル読み込み
//オプションのInJustDecodeBoundsがtrueのため実際の画像はメモリに読み込まれません。
ImageService.GetBitmap(filePath, options);
//引数の縦幅に合わせた画像サイズを再計算します。
//The with and height of the elements will be used to decode the image
if (width > 0 && height > 0)
{
options.InSampleSize = ImageService.CalculateInSampleSize(options, width, height);
}
else
{
options.InSampleSize = ImageService.CalculateInSampleSize(options, options.OutWidth, options.OutHeight);
}
//読み込む画像の階調を指定して読み込む
options.InPreferredConfig = Bitmap.Config.Argb8888;
//画像をメモリに読み込む場合はfalseを指定
options.InJustDecodeBounds = false;
//これで指定した縮尺で画像を読み込めます。
//容量も小さくなるのでOutOfMemoryが発生しにくいです。
bitmap = ImageService.GetBitmap(filePath, options);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine +
ex.StackTrace);
}
return bitmap;
}
private static Bitmap GetBitmap(string filePath, BitmapFactory.Options options)
{
Bitmap bitmap = null;
try
{
if (!String.IsNullOrEmpty(filePath))
{
bitmap = BitmapFactory.DecodeFile(filePath, options);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine +
ex.StackTrace);
}
return bitmap;
}
private static int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
float height = options.OutHeight;
float width = options.OutWidth;
var inSampleSize = 1D;
if (height > reqHeight || width > reqWidth)
{
int halfHeight = (int)(height / 2);
int halfWidth = (int)(width / 2);
// Calculate a inSampleSize that is a power of 2 - the decoder will use a value that is a power of two anyway.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
{
inSampleSize *= 2;
}
}
return (int)inSampleSize;
}
}
if (this.imgPicture.Source.GetType() == typeof(FileImageSource))
{
FileImageSource fis = (FileImageSource)this.imgPicture.Source;
//Androidの場合、画像を圧縮する
if (Device.RuntimePlatform == Device.Android)
{
Stream stream = DependencyService.Get<IImageService>().GetShrinkedStream(
fis.File,
(int)(this.imgPicture.WidthRequest),
(int)(this.imgPicture.HeightRequest)
);
//ストリームをバイト配列に変換する。
//このバイト配列をDB等に保存します。
byte[] smallSize = ImgConverter.GetByteArrayFromStream(stream);
//元サイズとの比較を出力
byte[] normalSize = DependencyService.Get<IFileService>().GetBytesFromFile(fis.File);
Debug.WriteLine("●normalSize Length=" + normalSize.Length.ToString() +
" StringLength=" + System.Convert.ToBase64String(normalSize).Length.ToString());
Debug.WriteLine("●smallSize Length=" + smallSize.Length.ToString() +
" StringLength=" + System.Convert.ToBase64String(smallSize).Length.ToString());
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。