@TargetApi(10) private Bitmap decodeRegionCrop(Bitmap croppedImage, Rect rect) { // Release memory now clearImageView(); InputStream is = null; try { is = getContentResolver().openInputStream(sourceUri); BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false); final int width = decoder.getWidth(); final int height = decoder.getHeight(); if (exifRotation != 0) { // Adjust crop area to account for image rotation Matrix matrix = new Matrix(); matrix.setRotate(-exifRotation); RectF adjusted = new RectF(); matrix.mapRect(adjusted, new RectF(rect)); // Adjust to account for origin at 0,0 adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0); rect = new Rect( (int) adjusted.left, (int) adjusted.top, (int) adjusted.right, (int) adjusted.bottom); } try { croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options()); } catch (IllegalArgumentException e) { // Rethrow with some extra information throw new IllegalArgumentException( "Rectangle " + rect + " is outside of the image (" + width + "," + height + "," + exifRotation + ")", e); } } catch (IOException e) { Log.e("Error cropping image: " + e.getMessage(), e); finish(); } catch (OutOfMemoryError e) { Log.e("OOM cropping image: " + e.getMessage(), e); setResultException(e); } finally { CropUtil.closeSilently(is); } return croppedImage; }
@Override public BitmapRegionDecoder run(JobContext jc) { if (!prepareInputFile(jc)) return null; BitmapRegionDecoder decoder = DecodeUtils.createBitmapRegionDecoder(jc, mFileDescriptor.getFileDescriptor(), false); mWidth = decoder.getWidth(); mHeight = decoder.getHeight(); return decoder; }
private void updateTileProvider(ImageEntry entry) { Bitmap screenNail = entry.screenNail; BitmapRegionDecoder fullImage = entry.fullImage; if (screenNail != null) { if (fullImage != null) { mTileProvider.setBackupImage(screenNail, fullImage.getWidth(), fullImage.getHeight()); mTileProvider.setRegionDecoder(fullImage); } else { int width = screenNail.getWidth(); int height = screenNail.getHeight(); mTileProvider.setBackupImage(screenNail, width, height); } } else { mTileProvider.clear(); if (entry.failToLoad) mTileProvider.setFailedToLoad(); } }
boolean decodeJPEGAndAnalyze(Context context, CalibrationEntries calibrationEntries) { boolean result = false; BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferQualityOverSpeed = true; BitmapRegionDecoder brd; byte[] data = mData; try { brd = BitmapRegionDecoder.newInstance(data, 0, data.length, false); Rect rect; rect = new Rect(0, 0, brd.getWidth(), brd.getHeight()); Bitmap bitmap = brd.decodeRegion(rect, opt); MatBitmapHolder mRgbaMatHolder = new MatBitmapHolder(bitmap); Mat rgbaMat = mRgbaMatHolder.pin(); Mat grayData = new Mat(); Imgproc.cvtColor(rgbaMat, grayData, Imgproc.COLOR_RGB2GRAY, 1); Mat centersCalibCircles = new Mat(); Size patternSize = CalibrationEntries.Pattern_ASYMMETRIC_CIRCLES_GRID_SIZE; Size imageSize = new Size(grayData.cols(), grayData.rows()); boolean patternWasFound = Calib3d.findCirclesGridDefault( grayData, patternSize, centersCalibCircles, Calib3d.CALIB_CB_ASYMMETRIC_GRID); if (patternWasFound && this.orientation != null) { int addedIdx = calibrationEntries.addEntry(this.orientation, centersCalibCircles); if (addedIdx >= 0) { Log.d( "CALIB", String.format( "PictureCapture: Added calibration entry at %d tot: %d", addedIdx, calibrationEntries.getNumEntries())); if (calibrationEntries.getNewlyAdded() > 5) { List<Mat> imagePoints = calibrationEntries.getPoints(); List<Mat> objectPoints = calibrationEntries.getObjectPointsAsymmentricList(imagePoints.size()); if (CalibrationEntries.isEnoughCalibrationPoints(imagePoints.size())) { calibrationEntries.resetNewlyAdded(); CameraCalibrationData cameraCalibrationData = new CameraCalibrationData(); List<Mat> rvecs = new Vector<Mat>(imagePoints.size()); List<Mat> tvecs = new Vector<Mat>(imagePoints.size()); int flags = 0; flags |= Calib3d.CALIB_FIX_K4 | Calib3d.CALIB_FIX_K5; Log.d("CALIB", String.format("PictureCapture: Calling Calib3d.calibrateCamera")); Mat K = new Mat(); Mat kdist = new Mat(); double rms = Calib3d.calibrateCamera( objectPoints, imagePoints, imageSize, K, kdist, rvecs, tvecs, flags); double[] Karray = new double[9]; double[] distcoeffs_array = new double[5]; K.get(0, 0, Karray); kdist.get(0, 0, distcoeffs_array); cameraCalibrationData.setData( grayData.cols(), grayData.rows(), Karray, distcoeffs_array, rms); Log.d( "CALIB", String.format( "PictureCapture: Calibration data: %s", cameraCalibrationData.formatCalibrationDataString())); calibrationEntries.setCalibrationData(cameraCalibrationData); result = true; } } } } // mRightBitmap = brd.decodeRegion(rect, opt); rect = new Rect(brd.getWidth() - brd.getWidth() / 3, 0, brd.getWidth(), brd.getHeight()); // mLeftBitmap = brd.decodeRegion(rect, opt); mRgbaMatHolder.unpin(rgbaMat); } catch (IOException e) { XLog.e("Region decoder doesn't want to cooperate", e); } return result; }