void UploadBytes(ref byte[] pixels, int width, int height) { ImagePixels ip = new ImagePixels(width, height, pixels); ParameterizedThreadStart ts = new ParameterizedThreadStart(CountryRecognition); Thread thread = new Thread(ts); thread.Start(ip); }
/// <summary> /// byte配列からWriteableBitmap(BitmapSource)を作成する /// </summary> /// <param name="image"></param> /// <returns></returns> private static WriteableBitmap CreateWriteableBitmap(ImagePixels image) { const double dpi = 96.0; var pixelFormat = PixelFormats.Bgra32; var bitmap = new WriteableBitmap(image.Width, image.Height, dpi, dpi, pixelFormat, null); bitmap.WritePixels( new Int32Rect(0, 0, image.Width, image.Height), image.Pixels, image.Stride, 0); return(bitmap); }
void CountryRecognition(System.Object obj) { ImagePixels ip = (ImagePixels)obj; #if UNITY_IPHONE && !UNITY_EDITOR sw.Start(); country_code = predict(ip.pixels, ip.height, ip.width, 400, 200, 200, multi_class_model_path, one_class_model_path, dict_path, 0.4, ref prob); sw.Stop(); //UnityEngine.Debug.Log ("Recognition cost: "+sw.ElapsedMilliseconds+" ms\n"); #else country_code = 1; #endif sw.Reset(); setRecognitionStatus(RECOG_STATUS.FINISHED); }
public ImageStatistic(ImagePixels img) { Histogram = new BitmapHistogram(); img.ForEachOnPixel(DoHistogram); }
// // FNA3D_Image_Free // public static void FNA3D_Image_Free(IntPtr mem) { // FNA calls this method after uploading the data returned by // ReadImageStream, so we can safely discard the reference ImagePixels.set(null); }
// // ReadImageStream // public static IntPtr ReadImageStream(System.IO.Stream stream, out int width, out int height, out int len, int forceWidth, int forceHeight, bool zoom) { var bitmap = LoadBitmap(stream); int[] pixels; if (forceWidth == -1 || forceHeight == -1) { pixels = GetPixels(bitmap, out width, out height, out len); } else { pixels = CropAndScale(bitmap, forceWidth, forceHeight, zoom, out width, out height, out len); } // keep a strong reference until FNA3D_Image_Free is called ImagePixels.set(pixels); return(System.Runtime.InteropServices.GCHandle.Alloc( pixels, System.Runtime.InteropServices.GCHandleType.Pinned) .AddrOfPinnedObject()); android.graphics.Bitmap LoadBitmap(System.IO.Stream stream) { if (stream is Microsoft.Xna.Framework.TitleContainer.TitleStream titleStream) { var bitmap = android.graphics.BitmapFactory .decodeStream(titleStream.JavaStream); if (bitmap == null || bitmap.getConfig() != android.graphics.Bitmap.Config.ARGB_8888) { string reason = (bitmap == null) ? "unspecified error" : $"unsupported config '{bitmap.getConfig()}'"; throw new BadImageFormatException( $"Load failed for bitmap image '{titleStream.Name}': {reason}"); } return(bitmap); } throw new ArgumentException(stream?.GetType()?.ToString()); } int[] CropAndScale(android.graphics.Bitmap bitmap, int newWidth, int newHeight, bool zoom, out int width, out int height, out int len) { int oldWidth = bitmap.getWidth(); int oldHeight = bitmap.getHeight(); bool scaleWidth = zoom ? (oldWidth < oldHeight) : (oldWidth > oldHeight); float scaleFactor = scaleWidth ? ((float)newWidth / (float)oldWidth) : ((float)newHeight / (float)oldHeight); if (zoom) { int x, y, w, h; if (scaleWidth) { x = 0; y = (int)(oldHeight / 2 - (newHeight / scaleFactor) / 2); w = oldWidth; h = (int)(newHeight / scaleFactor); } else { x = (int)(oldWidth / 2 - (newWidth / scaleFactor) / 2); y = 0; w = (int)(newWidth / scaleFactor); h = oldHeight; } bitmap = android.graphics.Bitmap.createBitmap(bitmap, x, y, w, h); } else { newWidth = (int)(oldWidth * scaleFactor); newHeight = (int)(oldHeight * scaleFactor); } return(GetPixels(android.graphics.Bitmap.createScaledBitmap( bitmap, newWidth, newHeight, false), out width, out height, out len)); } int[] GetPixels(android.graphics.Bitmap bitmap, out int width, out int height, out int len) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); var pixels = new int[w * h]; bitmap.copyPixelsToBuffer(java.nio.IntBuffer.wrap(pixels)); width = w; height = h; len = w * h * 4; return(pixels); } }