List of usage examples for android.graphics Matrix setRectToRect
public boolean setRectToRect(RectF src, RectF dst, ScaleToFit stf)
From source file:com.digitalvotingpass.camera.CameraFragment.java
/** * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. * This method should be called after the camera preview size is determined in * setUpCameraOutputs and also the size of `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` *///from w w w. ja v a 2s. com public void configureTransform(int viewWidth, int viewHeight) { Activity activity = getActivity(); if (null == mTextureView || null == mPreviewSize || null == activity) { return; } int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = Math.max((float) viewHeight / mPreviewSize.getHeight(), (float) viewWidth / mPreviewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } else if (Surface.ROTATION_180 == rotation) { matrix.postRotate(180, centerX, centerY); } mTextureView.setTransform(matrix); overlay.setRect(CameraFragmentUtil.getScanRect(scanSegment)); }
From source file:com.raulh82vlc.face_detection_sample.camera2.presentation.FDCamera2Presenter.java
/** * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. * This method should be called after the camera preview size is determined in * setUpCameraOutputs and also the size of `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` *///from w ww . jav a 2 s.c om private void configureTransform(int viewWidth, int viewHeight) { if (isViewAvailable()) { if (null == activityView.getTextureView()) { return; } int rotation = activityView.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth()); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = Math.max((float) viewHeight / previewSize.getHeight(), (float) viewWidth / previewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } else if (Surface.ROTATION_180 == rotation) { matrix.postRotate(180, centerX, centerY); } activityView.getTextureView().setTransform(matrix); } }
From source file:com.dynamixsoftware.printingsample.PrintServiceFragment.java
@Override public void onClick(View v) { final Context appContext = requireContext().getApplicationContext(); switch (v.getId()) { case R.id.set_license: printingSdk.setLicense("YOUR_LICENSE_HERE", new ISetLicenseCallback.Stub() { @Override/*from ww w . j a va 2 s.co m*/ public void start() { Toast.makeText(appContext, "set license start", Toast.LENGTH_SHORT).show(); } @Override public void serverCheck() { Toast.makeText(appContext, "set license check server", Toast.LENGTH_SHORT).show(); } @Override public void finish(Result result) { Toast.makeText(appContext, "set license finish " + (result == Result.OK ? "ok" : "error"), Toast.LENGTH_SHORT).show(); } }); break; case R.id.init_current_and_recent_printers: try { printingSdk.initRecentPrinters(new ISetupPrinterListener.Stub() { @Override public void start() { toastInMainThread(appContext, "ISetupPrinterListener start"); } @Override public void libraryPackInstallationProcess(int arg0) { toastInMainThread(appContext, "ISetupPrinterListener libraryPackInstallationProcess " + arg0 + " %"); } @Override public void finish(Result arg0) { toastInMainThread(appContext, "ISetupPrinterListener finish " + arg0.name()); if (arg0.getType().equals(ResultType.ERROR_LIBRARY_PACK_NOT_INSTALLED)) { // printingSdk.setup should be called with forceInstall = true to download required drivers } } }); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.get_current_printer: try { Printer currentPrinter = printingSdk.getCurrentPrinter(); showDialog(getString(R.string.success), "Current printer:\n" + (currentPrinter != null ? currentPrinter.getName() : "null")); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.get_recent_printers: try { List<Printer> recentPrinters = printingSdk.getRecentPrintersList(); String message = ""; for (Printer printer : recentPrinters) message += printer.getName() + "\n"; if (message.length() == 0) message = "No recent printers"; showDialog(getString(R.string.success), message); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.discover_wifi: try { printingSdk.startDiscoverWiFi(new IDiscoverListener.Stub() { @Override public void start() { toastInMainThread(appContext, "IDiscoverListener start"); } @Override public void printerFound(List<Printer> arg0) { toastInMainThread(appContext, "IDiscoverListener printerFound"); discoveredPrinters.clear(); discoveredPrinters.addAll(arg0); } @Override public void finish(Result arg0) { toastInMainThread(appContext, "IDiscoverListener finish " + arg0.name()); } }); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.discover_bluetooth: try { printingSdk.startDiscoverBluetooth(new IDiscoverListener.Stub() { @Override public void start() { toastInMainThread(appContext, "IDiscoverListener start"); } @Override public void printerFound(List<Printer> arg0) { toastInMainThread(appContext, "IDiscoverListener printerFound"); discoveredPrinters.clear(); discoveredPrinters.addAll(arg0); } @Override public void finish(Result arg0) { toastInMainThread(appContext, "IDiscoverListener finish " + arg0.name()); } }); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.discover_google_cloud: try { printingSdk.startDiscoverCloud("YOUR_GOOGLE_ACCOUNT_NAME", new IDiscoverCloudListener.Stub() { @Override public void start() { toastInMainThread(appContext, "IDiscoverCloudListener start"); } @Override public void showAuthorization(Intent arg0) { // Launch Intent arg0 to show authorization activity } @Override public void printerFound(List<Printer> arg0) { toastInMainThread(appContext, "IDiscoverCloudListener printerFound"); discoveredPrinters.clear(); discoveredPrinters.addAll(arg0); } @Override public void finish(Result arg0) { toastInMainThread(appContext, "IDiscoverCloudListener finish " + arg0.name()); } }); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.discover_smb: try { discoverSmb = printingSdk.startDiscoverSmb(new IDiscoverSmbListener.Stub() { @Override public void start() { toastInMainThread(appContext, "IDiscoverSmbListener start"); } @Override public void smbFilesFound(List<SmbFile> arg0) { // Show list of SMB files. This listener is used for navigation. // You should call discoverSmbControl.move(arg0) to change location. } @Override public void showAuthorization() { // You have to ask user for authorization credentials and call discoverSmbControl.login(arg0, arg1); } @Override public void printerFound(List<Printer> arg0) { toastInMainThread(appContext, "IDiscoverSmbListener printerFound"); discoveredPrinters.clear(); discoveredPrinters.addAll(arg0); } @Override public void finish(Result arg0) { toastInMainThread(appContext, "IDiscoverSmbListener finish " + arg0.name()); } }); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.discover_usb: try { printingSdk.startDiscoverUSB(new IDiscoverListener.Stub() { @Override public void start() { toastInMainThread(appContext, "IDiscoverListener start"); } @Override public void printerFound(List<Printer> arg0) { toastInMainThread(appContext, "IDiscoverListener printerFound"); discoveredPrinters.clear(); discoveredPrinters.addAll(arg0); } @Override public void finish(Result arg0) { toastInMainThread(appContext, "IDiscoverListener finish " + arg0.name()); } }); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.find_driver: if (!discoveredPrinters.isEmpty()) { Printer printer = discoveredPrinters.get(0); try { printingSdk.findDrivers(printer, new IFindDriversListener.Stub() { @Override public void start() { toastInMainThread(appContext, "IFindDriversListener start"); } @Override public void finish(List<DriversSearchEntry> arg0) { toastInMainThread(appContext, "IFindDriversListener finish; Found " + arg0.size() + " drivers entries;" + ((arg0.size() == 0) ? "" : "")); driversSearchEntries.clear(); driversSearchEntries.addAll(arg0); } }); } catch (RemoteException e) { e.printStackTrace(); } } else showDialog(getString(R.string.error), "Discover printers first"); break; case R.id.get_drivers: if (!discoveredPrinters.isEmpty()) { Printer printer = discoveredPrinters.get(0); TransportType transportType = printer.getTransportTypes().get(0); if (transportType != null) { try { printingSdk.getDriversList(printer, transportType, new IGetDriversListener.Stub() { @Override public void start() { toastInMainThread(appContext, "IGetDriversListener start"); } @Override public void finish(List<DriverHandleEntry> arg0) { toastInMainThread(appContext, "IGetDriversListener finish"); driverHandleEntries.clear(); driverHandleEntries.addAll(arg0); } }); } catch (RemoteException e) { e.printStackTrace(); } } } else showDialog(getString(R.string.error), "Discover printers first"); break; case R.id.setup_recent_printer: try { List<Printer> printerList = printingSdk.getRecentPrintersList(); if (!printerList.isEmpty()) printingSdk.setup(printerList.get(0), true, new ISetupPrinterListener.Stub() { @Override public void start() { toastInMainThread(appContext, "ISetupPrinterListener start"); } @Override public void libraryPackInstallationProcess(int arg0) { toastInMainThread(appContext, "ISetupPrinterListener libraryPackInstallationProcess " + arg0 + " %"); } @Override public void finish(Result arg0) { toastInMainThread(appContext, "ISetupPrinterListener finish " + arg0.name()); if (arg0.getType().equals(ResultType.ERROR_LIBRARY_PACK_NOT_INSTALLED)) { // printingSdk.setup should be called with forceInstall = true to download required drivers } } }); else showDialog(getString(R.string.error), "No recent printers"); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.setup_discovered_printer: if (!discoveredPrinters.isEmpty()) { if (!driversSearchEntries.isEmpty()) { Printer printer = discoveredPrinters.get(0); DriversSearchEntry driversSearchEntry = driversSearchEntries.get(0); try { printingSdk.setup(printer, driversSearchEntry.getDriverHandlesList().get(0), driversSearchEntry.getTransportType(), false, new ISetupPrinterListener.Stub() { @Override public void start() { toastInMainThread(appContext, "ISetupPrinterListener start"); } @Override public void libraryPackInstallationProcess(int arg0) { toastInMainThread(appContext, "ISetupPrinterListener libraryPackInstallationProcess " + arg0 + " %"); } @Override public void finish(Result arg0) { toastInMainThread(appContext, "ISetupPrinterListener finish " + arg0.name()); if (arg0.getType().equals(ResultType.ERROR_LIBRARY_PACK_NOT_INSTALLED)) { // printingSdk.setup should be called with forceInstall = true to download required drivers } } }); } catch (RemoteException e) { e.printStackTrace(); } } else showDialog(getString(R.string.error), "Find driver first"); } else showDialog(getString(R.string.error), "Discover printers first"); break; case R.id.change_options: try { Printer currentPrinter = printingSdk.getCurrentPrinter(); if (currentPrinter != null) { List<PrinterOption> options = currentPrinter.getOptions(); if (options.size() > 0) { Random random = new Random(); PrinterOption option = options.get(random.nextInt(options.size())); PrinterOptionValue currentValue = option.getOptionValue(); List<PrinterOptionValue> valuesList = option.getOptionValueList(); PrinterOptionValue newValue = valuesList.get(random.nextInt(valuesList.size())); printingSdk.setCurrentPrinterOptionValue(option, newValue); Toast.makeText(requireContext().getApplicationContext(), "option " + option.getName() + " changed from " + currentValue + " to " + newValue, Toast.LENGTH_LONG).show(); } } else showDialog(getString(R.string.error), "Setup printer first"); } catch (RemoteException e) { e.printStackTrace(); } break; case R.id.print_image: try { if (printingSdk.getCurrentPrinter() != null) { List<IPage> pages = new ArrayList<>(); pages.add(new IPage() { @Override public Bitmap getBitmapFragment(Rect fragment) { Printer printer = null; try { printer = printingSdk.getCurrentPrinter(); } catch (RemoteException e) { e.printStackTrace(); } 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.getContext().getHResolution(); int yDpi = printer.getContext().getVResolution(); // in dots int paperWidth = printer.getContext().getPaperWidth() * xDpi / 72; int paperHeight = printer.getContext().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; } }); try { printingSdk.print(pages, 1, new IPrintListener.Stub() { @Override public void startingPrintJob() { toastInMainThread(appContext, "IPrintListener startingPrintJob"); } @Override public void start() { toastInMainThread(appContext, "IPrintListener start"); } @Override public void sendingPage(int arg0, int arg1) { toastInMainThread(appContext, "IPrintListener sendingPage " + arg0 + "; progress " + arg1 + "%"); } @Override public void preparePage(int arg0) { toastInMainThread(appContext, "IPrintListener preparePage " + arg0); } @Override public boolean needCancel() { toastInMainThread(appContext, "IPrintListener needCancel"); // Return false if cancel needed. return false; } @Override public void finishingPrintJob() { toastInMainThread(appContext, "IPrintListener finishingPrintJob"); } @Override public void finish(Result arg0, int arg1, int arg2) { toastInMainThread(appContext, "IPrintListener finish Result " + arg0 + "; Result type " + arg0.getType() + "; Total pages " + arg1 + "; Pages sent " + arg2); } }); } catch (RemoteException e) { e.printStackTrace(); } } else showDialog(getString(R.string.error), "You must setup printer before print"); } catch (RemoteException e) { e.printStackTrace(); } break; } }
From source file:com.example.android.tflitecamerademo.Camera2BasicFragment.java
/** * Configures the necessary {@link android.graphics.Matrix} transformation to `textureView`. This * method should be called after the camera preview size is determined in setUpCameraOutputs and * also the size of `textureView` is fixed. * * @param viewWidth The width of `textureView` * @param viewHeight The height of `textureView` *//*from ww w .j a va 2 s .c o m*/ private void configureTransform(int viewWidth, int viewHeight) { Activity activity = getActivity(); if (null == textureView || null == previewSize || null == activity) { return; } int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth()); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = Math.max((float) viewHeight / previewSize.getHeight(), (float) viewWidth / previewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } else if (Surface.ROTATION_180 == rotation) { matrix.postRotate(180, centerX, centerY); } textureView.setTransform(matrix); }
From source file:com.tzutalin.dlibtest.CameraConnectionFragment.java
/** * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. * This method should be called after the camera preview size is determined in * setUpCameraOutputs and also the size of `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` */// w ww .j ava 2 s . co m @DebugLog private void configureTransform(final int viewWidth, final int viewHeight) { final Activity activity = getActivity(); if (null == textureView || null == previewSize || null == activity) { return; } final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); final Matrix matrix = new Matrix(); final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); final RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth()); final float centerX = viewRect.centerX(); final float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); final float scale = Math.max((float) viewHeight / previewSize.getHeight(), (float) viewWidth / previewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } else if (Surface.ROTATION_180 == rotation) { matrix.postRotate(180, centerX, centerY); } textureView.setTransform(matrix); }
From source file:org.odk.collect.android.fragments.Camera2VideoFragment.java
/** * Configures the necessary {@link android.graphics.Matrix} transformation to `textureView`. * This method should not to be called until the camera preview size is determined in * openCamera, or until the size of `textureView` is fixed. * * @param viewWidth The width of `textureView` * @param viewHeight The height of `textureView` */// www. j a v a 2 s .c o m private void configureTransform(int viewWidth, int viewHeight) { Activity activity = getActivity(); if (null == textureView || null == previewSize || null == activity) { return; } int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth()); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = Math.max((float) viewHeight / previewSize.getHeight(), (float) viewWidth / previewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } textureView.setTransform(matrix); }
From source file:com.android.rahul.myselfieapp.Fragment.CamVideoFragment.java
/** * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. * This method should not to be called until the camera preview size is determined in * openCamera, or until the size of `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` *//*from ww w . j a va2s . c o m*/ private void configureTransform(int viewWidth, int viewHeight) { Activity activity = getActivity(); if (null == mTextureView || null == mPreviewSize || null == activity) { return; } int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = Math.max((float) viewHeight / mPreviewSize.getHeight(), (float) viewWidth / mPreviewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } mTextureView.setTransform(matrix); }
From source file:com.caseystalnaker.android.popinvideodemo.fragments.Camera2VideoFragment.java
/** * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. * This method should not to be called until the camera preview size is determined in * openCamera, or until the size of `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` *//*from ww w.j av a2 s .c om*/ private void configureTransform(final int viewWidth, final int viewHeight) { Activity activity = getActivity(); if (null == mTextureView || null == mPreviewSize || null == activity) { return; } final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); final Matrix matrix = new Matrix(); final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); final RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); final float centerX = viewRect.centerX(); final float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); final float scale = Math.max((float) viewHeight / mPreviewSize.getHeight(), (float) viewWidth / mPreviewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } mTextureView.setTransform(matrix); }
From source file:com.tenth.space.ui.fragment.HomeFragment.java
private void configureTransform(int viewWidth, int viewHeight) { Activity activity = getActivity();//ww w.java2 s . c o m if (null == mTextureView || null == mPreviewSize || null == activity) { return; } int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = Math.max((float) viewHeight / mPreviewSize.getHeight(), (float) viewWidth / mPreviewSize.getWidth()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } else if (Surface.ROTATION_180 == rotation) { matrix.postRotate(180, centerX, centerY); } mTextureView.setTransform(matrix); }
From source file:com.iwuvhugs.seekgame.PhotoFragment.java
/** * Configures the necessary {@link Matrix} transformation to `mTextureView`. * This method should be called after the camera preview size is determined in * setUpCameraOutputs and also the size of `mTextureView` is fixed. * * @param viewWidth The width of `mTextureView` * @param viewHeight The height of `mTextureView` *//*from w ww . j av a2s . c om*/ private void configureTransform(int viewWidth, int viewHeight) { Activity activity = getActivity(); if (null == mTextureView || null == mPreviewSize || null == activity) { return; } int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); float centerX = viewRect.centerX(); float centerY = viewRect.centerY(); if (Surface.ROTATION_0 == rotation) { bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); float scale = (float) (viewWidth / mPreviewSize.getWidth() + 0.6); matrix.postScale(scale, scale, centerX, centerY); } mTextureView.setTransform(matrix); }