List of usage examples for android.graphics Matrix setRectToRect
public boolean setRectToRect(RectF src, RectF dst, ScaleToFit stf)
From source file:Main.java
/** * Scale a bitmap and correct the dimensions * * @param bitmap Bitmap to scale//from ww w . j a v a 2 s . co m * @param width width for scaling * @param height height for scaling * @param orientation Current orientation of the Image * @return Scaled bitmap */ public static Bitmap getScaledBitmap(Bitmap bitmap, int width, int height, int orientation) { Matrix m = new Matrix(); m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), new RectF(0, 0, width, height), Matrix.ScaleToFit.CENTER); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { m.postRotate(ORIENTATION_90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { m.postRotate(ORIENTATION_180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { m.postRotate(ORIENTATION_270); } return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); }
From source file:Main.java
public static Bitmap createCircularClip(Bitmap input, int width, int height) { if (input == null) return null; final int inWidth = input.getWidth(); final int inHeight = input.getHeight(); final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(output); final Paint paint = new Paint(); paint.setShader(new BitmapShader(input, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); paint.setAntiAlias(true);// w w w. j a va 2 s. c o m final RectF srcRect = new RectF(0, 0, inWidth, inHeight); final RectF dstRect = new RectF(0, 0, width, height); final Matrix m = new Matrix(); m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER); canvas.setMatrix(m); canvas.drawCircle(inWidth / 2, inHeight / 2, inWidth / 2, paint); return output; }
From source file:Main.java
public static boolean setBitmapToDisplayMatrix(Matrix m, RectF imageBounds, RectF displayBounds) { m.reset();/*from w w w.j a va 2 s . c o m*/ return m.setRectToRect(imageBounds, displayBounds, Matrix.ScaleToFit.CENTER); }
From source file:Main.java
public static Bitmap scale(Bitmap b, int reqWidth, int reqHeight) { Matrix m = new Matrix(); if (b.getWidth() > b.getHeight()) { reqWidth = (int) (reqHeight * (1.0 * b.getWidth() / b.getHeight())); } else {// w ww . jav a 2s .co m reqHeight = (int) (reqWidth * (1.0 * b.getHeight() / b.getWidth())); } m.setRectToRect(new RectF(0, 0, b.getWidth(), b.getHeight()), new RectF(0, 0, reqWidth, reqHeight), Matrix.ScaleToFit.CENTER); return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true); }
From source file:Main.java
public static Bitmap resizeBitmapToImageView(String path) { Bitmap resizedBitmap = null;/*from ww w . ja v a 2s.com*/ try { int inWidth; int inHeight; int dstWidth = 1000; int dstHeight = 1000; InputStream in = new FileInputStream(path); // decode image size (decode metadata only, not the whole image) BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); // save width and height inWidth = options.outWidth; inHeight = options.outHeight; // decode full image pre-resized in = new FileInputStream(path); options = new BitmapFactory.Options(); // calc rought re-size (this is no exact resize) options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight); // decode full image Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options); // calc exact destination size Matrix m = new Matrix(); RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight()); RectF outRect = new RectF(0, 0, dstWidth, dstHeight); m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER); float[] values = new float[9]; m.getValues(values); // resize bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true); // save image // try { // FileOutputStream out = new FileOutputStream(path); // resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); // } catch (Exception e) { //// Log.e("Image", e.getMessage(), e); // } } catch (IOException e) { // Log.e("Image", e.getMessage(), e); } return resizedBitmap; }
From source file:Main.java
private static void saveSmallerImage(String pathOfInputImage, int dstWidth, int dstHeight) { try {//from ww w. j a v a 2 s. c om int inWidth = 0; int inHeight = 0; InputStream in = new FileInputStream(pathOfInputImage); // decode image size (decode metadata only, not the whole image) BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); in = null; // save width and height inWidth = options.outWidth; inHeight = options.outHeight; // decode full image pre-resized in = new FileInputStream(pathOfInputImage); options = new BitmapFactory.Options(); // calc rought re-size (this is no exact resize) options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight); // decode full image Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options); // calc exact destination size Matrix m = new Matrix(); RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight()); RectF outRect = new RectF(0, 0, dstWidth, dstHeight); m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER); float[] values = new float[9]; m.getValues(values); // resize bitmap Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true); // save image try { FileOutputStream out = new FileOutputStream(pathOfInputImage); resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); } catch (Exception e) { Log.e("Image", e.getMessage(), e); } } catch (IOException e) { Log.e("Image", e.getMessage(), e); } catch (Exception e) { Log.e("Image", e.getMessage()); } }
From source file:Main.java
public static boolean setImageToScreenMatrix(Matrix dst, RectF image, RectF screen, int rotation) { RectF rotatedImage = new RectF(); dst.setRotate(rotation, image.centerX(), image.centerY()); if (!dst.mapRect(rotatedImage, image)) { return false; // fails for rotations that are not multiples of 90 // degrees }//from w w w .java 2s. com boolean rToR = dst.setRectToRect(rotatedImage, screen, Matrix.ScaleToFit.CENTER); boolean rot = dst.preRotate(rotation, image.centerX(), image.centerY()); return rToR && rot; }
From source file:de.wikilab.android.friendica01.Max.java
public static void resizeImage(String pathOfInputImage, String pathOfOutputImage, int dstWidth, int dstHeight) { try {/*from w w w .ja v a 2 s. co m*/ int inWidth = 0; int inHeight = 0; InputStream in = new FileInputStream(pathOfInputImage); // decode image size (decode metadata only, not the whole image) BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); in = null; // save width and height inWidth = options.outWidth; inHeight = options.outHeight; // decode full image pre-resized in = new FileInputStream(pathOfInputImage); options = new BitmapFactory.Options(); // calc rought re-size (this is no exact resize) options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight); // decode full image Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options); // calc exact destination size Matrix m = new Matrix(); RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight()); RectF outRect = new RectF(0, 0, dstWidth, dstHeight); m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER); float[] values = new float[9]; m.getValues(values); // resize bitmap Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true); // save image try { FileOutputStream out = new FileOutputStream(pathOfOutputImage); resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); } catch (Exception e) { Log.e("Image", e.getMessage(), e); } } catch (IOException e) { Log.e("Image", e.getMessage(), e); } }
From source file:vi.pdfscanner.fragment.CropFragment.java
private Bitmap scaledBitmap(Bitmap bitmap, int width, int height) { Matrix m = new Matrix(); m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), new RectF(0, 0, width, height), Matrix.ScaleToFit.CENTER);// ww w. j av a2 s.com return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); }
From source file:com.dynamixsoftware.printingsample.IntentApiFragment.java
@Override public void onClick(View v) { switch (v.getId()) { case R.id.check_premium: new AlertDialog.Builder(requireContext()).setTitle(R.string.check_premium) .setMessage("" + intentApi.checkPremium()).setPositiveButton(R.string.ok, null).show(); break;//from www.j av a 2 s . c o m case R.id.activate_online: final Context appContext = requireContext().getApplicationContext(); intentApi.setLicense("YOUR_ACTIVATION_KEY", new ISetLicenseCallback.Stub() { @Override public void start() { toastInMainThread(appContext, "activate start"); } @Override public void serverCheck() { toastInMainThread(appContext, "activate check server"); } @Override public void finish(final Result arg0) { toastInMainThread(appContext, "activate finish " + (arg0 == Result.OK ? "ok" : "error")); } }); break; case R.id.setup_printer: intentApi.setupCurrentPrinter(); break; case R.id.change_options: intentApi.changePrinterOptions(); break; case R.id.get_current_printer: try { IPrinterInfo printer = intentApi.getCurrentPrinter(); Toast.makeText(requireContext().getApplicationContext(), "current printer " + (printer != null ? printer.getName() : "null"), Toast.LENGTH_LONG) .show(); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_image: intentApi.print(Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG)), "image/png", "from printing sample"); break; case R.id.print_file: intentApi.print(Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_DOC)), "application/msword", "from printing sample"); break; case R.id.show_file_preview: intentApi.showFilePreview( Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_DOC)), "application/msword", 0); break; case R.id.print_with_your_rendering: try { IDocument.Stub document = new IDocument.Stub() { private int thumbnailWidth; private int thumbnailHeight; @Override public Bitmap renderPageFragment(int arg0, Rect fragment) throws RemoteException { IPrinterInfo printer = intentApi.getCurrentPrinter(); if (printer != null) { Bitmap bitmap = Bitmap.createBitmap(fragment.width(), fragment.height(), Bitmap.Config.ARGB_8888); for (int i = 0; i < 3; i++) try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; if (i > 0) { options.inSampleSize = 1 << i; } Bitmap imageBMP = BitmapFactory.decodeStream( new FileInputStream( FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG)), null, options); Paint p = new Paint(); int imageWidth = 0; int imageHeight = 0; if (imageBMP != null) { imageWidth = imageBMP.getWidth(); imageHeight = imageBMP.getHeight(); } int xDpi = printer.getPrinterContext().getHResolution(); int yDpi = printer.getPrinterContext().getVResolution(); // in dots int paperWidth = printer.getPrinterContext().getPaperWidth() * xDpi / 72; int paperHeight = printer.getPrinterContext().getPaperHeight() * yDpi / 72; float aspectH = (float) imageHeight / (float) paperHeight; float aspectW = (float) imageWidth / (float) paperWidth; aspectH = aspectH > 1 ? 1 / aspectH : aspectH; aspectW = aspectW > 1 ? 1 / aspectW : aspectW; RectF dst = new RectF(0, 0, fragment.width() * aspectW, fragment.height() * aspectH); float sLeft = 0; float sTop = fragment.top * aspectH; float sRight = imageWidth; float sBottom = fragment.top * aspectH + fragment.bottom * aspectH; RectF source = new RectF(sLeft, sTop, sRight, sBottom); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); // move image to actual printing area dst.offsetTo(dst.left - fragment.left, dst.top - fragment.top); Matrix matrix = new Matrix(); matrix.setRectToRect(source, dst, Matrix.ScaleToFit.FILL); canvas.drawBitmap(imageBMP, matrix, p); break; } catch (IOException ex) { ex.printStackTrace(); break; } catch (OutOfMemoryError ex) { if (bitmap != null) { bitmap.recycle(); bitmap = null; } continue; } return bitmap; } else { return null; } } @Override public void initDeviceContext(IPrinterContext printerContext, int thumbnailWidth, int thumbnailHeight) { this.thumbnailWidth = thumbnailWidth; this.thumbnailHeight = thumbnailHeight; } @Override public int getTotalPages() { return 1; } @Override public String getDescription() { return "PrintHand test page"; } @Override public Bitmap getPageThumbnail(int arg0) throws RemoteException { Bitmap bitmap = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.ARGB_8888); for (int i = 0; i < 3; i++) try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; if (i > 0) { options.inSampleSize = 1 << i; } Bitmap imageBMP = BitmapFactory.decodeStream( new FileInputStream( FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG)), null, options); Paint p = new Paint(); int imageWidth = 0; int imageHeight = 0; if (imageBMP != null) { imageWidth = imageBMP.getWidth(); imageHeight = imageBMP.getHeight(); } // default int paperWidth = 2481; int paperHeight = 3507; IPrinterInfo printer = intentApi.getCurrentPrinter(); if (printer != null) { int xDpi = printer.getPrinterContext().getHResolution(); int yDpi = printer.getPrinterContext().getVResolution(); // in dots paperWidth = printer.getPrinterContext().getPaperWidth() * xDpi / 72; paperHeight = printer.getPrinterContext().getPaperHeight() * yDpi / 72; } float aspectW = (float) imageWidth / (float) paperWidth; float aspectH = (float) imageHeight / (float) paperHeight; RectF dst = new RectF(0, 0, thumbnailWidth * aspectW, thumbnailHeight * aspectH); float sLeft = 0; float sTop = 0; float sRight = imageWidth; float sBottom = imageHeight; RectF source = new RectF(sLeft, sTop, sRight, sBottom); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); Matrix matrix = new Matrix(); matrix.setRectToRect(source, dst, Matrix.ScaleToFit.FILL); canvas.drawBitmap(imageBMP, matrix, p); break; } catch (IOException ex) { ex.printStackTrace(); break; } catch (OutOfMemoryError ex) { if (bitmap != null) { bitmap.recycle(); bitmap = null; } continue; } return bitmap; } }; intentApi.print(document); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_with_your_rendering_without_ui: try { IJob.Stub job = new IJob.Stub() { @Override public Bitmap renderPageFragment(int num, Rect fragment) throws RemoteException { IPrinterInfo printer = intentApi.getCurrentPrinter(); if (printer != null) { Bitmap bitmap = Bitmap.createBitmap(fragment.width(), fragment.height(), Bitmap.Config.ARGB_8888); for (int i = 0; i < 3; i++) try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; if (i > 0) { options.inSampleSize = 1 << i; } Bitmap imageBMP = BitmapFactory.decodeStream( new FileInputStream( FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG)), null, options); Paint p = new Paint(); int imageWidth = 0; int imageHeight = 0; if (imageBMP != null) { imageWidth = imageBMP.getWidth(); imageHeight = imageBMP.getHeight(); } int xDpi = printer.getPrinterContext().getHResolution(); int yDpi = printer.getPrinterContext().getVResolution(); // in dots int paperWidth = printer.getPrinterContext().getPaperWidth() * xDpi / 72; int paperHeight = printer.getPrinterContext().getPaperHeight() * yDpi / 72; float aspectH = (float) imageHeight / (float) paperHeight; float aspectW = (float) imageWidth / (float) paperWidth; RectF dst = new RectF(0, 0, fragment.width() * aspectW, fragment.height() * aspectH); float sLeft = 0; float sTop = fragment.top * aspectH; float sRight = imageWidth; float sBottom = fragment.top * aspectH + fragment.bottom * aspectH; RectF source = new RectF(sLeft, sTop, sRight, sBottom); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); // move image to actual printing area dst.offsetTo(dst.left - fragment.left, dst.top - fragment.top); Matrix matrix = new Matrix(); matrix.setRectToRect(source, dst, Matrix.ScaleToFit.FILL); canvas.drawBitmap(imageBMP, matrix, p); break; } catch (IOException ex) { ex.printStackTrace(); break; } catch (OutOfMemoryError ex) { if (bitmap != null) { bitmap.recycle(); bitmap = null; } continue; } return bitmap; } else { return null; } } @Override public int getTotalPages() { return 1; } }; intentApi.print(job, 1); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_image_with_print_hand_rendering_without_ui: try { intentApi.print("PrintingSample", "image/png", Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PNG))); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.change_image_options: try { List<PrintHandOption> imageOptions = intentApi.getImagesOptions(); changeRandomOption(imageOptions); intentApi.setImagesOptions(imageOptions); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_file_with_print_hand_rendering_without_ui: try { intentApi.print("PrintingSample", "application/ms-word", Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_DOC))); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_protected_file_with_print_hand_rendering_without_ui: try { intentApi.print("PrintingSample", "application/pdf", Uri.parse("file://" + FilesUtils.getFilePath(requireContext(), FilesUtils.FILE_PDF))); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.change_files_options: try { List<PrintHandOption> fileOptions = intentApi.getFilesOptions(); changeRandomOption(fileOptions); intentApi.setFilesOptions(fileOptions); } catch (RemoteException e) { e.printStackTrace(); } break; } }