private Bitmap decodeToBitmap(ByteBuffer jpegData, int sampleSize) { sbyte[] jpegDataArray = new sbyte[jpegData.remaining()]; jpegData.get(jpegDataArray); jpegData.rewind(); BitmapFactory.Options option = new BitmapFactory.Options(); option.inSampleSize = sampleSize; return BitmapFactory.decodeByteArray(jpegDataArray, 0, jpegDataArray.Length, option); }
private Bitmap decodeToBitmap(ByteBuffer jpegData, int sampleSize, int cropWidth) { sbyte[] jpegDataArray = new sbyte[jpegData.remaining()]; jpegData.get(jpegDataArray); jpegData.rewind(); BitmapFactory.Options option = new BitmapFactory.Options(); option.inSampleSize = sampleSize; if (cropWidth == 0) { return BitmapFactory.decodeByteArray(jpegDataArray, 0, jpegDataArray.Length, option); } Bitmap bitmap = null; try { BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(jpegDataArray, 0, jpegDataArray.Length, true); int cropHeight = cropWidth * decoder.Height / decoder.Width; Rect cropRect = new Rect(decoder.Width / 2 - cropWidth, decoder.Height / 2 - cropHeight, decoder.Width / 2 + cropWidth, decoder.Height / 2 + cropHeight); bitmap = decoder.decodeRegion(cropRect, option); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } return bitmap; }