Matrix m = new Matrix(); 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);
final RectF src = new RectF(0, 0, bm.getWidth(), bm.getHeight()); final RectF dst = new RectF(0, 0, dstWidth - dx, dstHeight - dy); scaleToFit.setRectToRect(src, dst, ScaleToFit.CENTER);
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);
Matrix matrix = new Matrix(); RectF from = new RectF(0, 0, bm.getIntrinsicWidth(), bm.getIntrinsicHeight()); RectF to = new RectF(view.getWidth() * 0.9, view.getHeight() * 0.7, view.getWidth(), view.getHeight()); matrix.setRectToRect(from, to, Matrix.ScaleToFit.FILL); view.setScaleType(ScaleType.MATRIX); view.setImageMatrix(matrix);
int sizeW=MediaStore.Images.Media.getBitmap(getContentResolver(), filePath).getWidth(); int sizeH=MediaStore.Images.Media.getBitmap(getContentResolver(), filePath).getHeight(); Matrix m = new Matrix(); m.setRectToRect(new RectF(0, 0,sizeW, sizeH), new RectF(0, 0, 300, 100), Matrix.ScaleToFit.FILL); Bitmap bm = Bitmap.createBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(), filePath), 0, 0, sizeW, sizeH, m, true); //Bitmap bm = Bitmap.createScaledBitmap(MediaStore.Images.Media.getBitmap(getContentResolver(), filePath), sizeW, sizeH, true); ByteArrayOutputStream bao = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 90, bao); byte[] ba = bao.toByteArray(); //Log.d("size200",String.valueOf((bm.getByteCount()/1024)/2)); ba1 = Base64.encodeToString(ba, Base64.DEFAULT); Log.e("base64-1", "-----" + ba1);
public static Bitmap scaleBitmapAndKeepRation(Bitmap TargetBmp,int reqHeightInPixels,int reqWidthInPixels) { Matrix m = new Matrix(); m.setRectToRect(new RectF(0, 0, TargetBmp.getWidth(), TargetBmp.getHeight()), new RectF(0, 0, reqWidthInPixels, reqHeightInPixels), Matrix.ScaleToFit.CENTER); Bitmap scaledBitmap = Bitmap.createBitmap(TargetBmp, 0, 0, TargetBmp.getWidth(), TargetBmp.getHeight(), m, true); return scaledBitmap; }
Matrix m = new Matrix(); m.setRectToRect(new RectF(0, 0, src.getWidth(), src.getHeight()), new RectF(0, 0, scaledWidth, scaledHeigth), Matrix.ScaleToFit.CENTER); Bitmap output = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); float imageWidth = image.getDrawable().getIntrinsicWidth(); float imageHeight = image.getDrawable().getIntrinsicHeight(); RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight); RectF viewRect = new RectF(0, 0, image.getWidth(), image.getHeight()); Matrix matrix = new Matrix(); matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER); image.setImageMatrix(matrix); image.invalidate(); }
imageView.setScaleType(ScaleType.MATRIX); Matrix matrix = new Matrix(); matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.FILL); imageView.setBitmap(bitmap);
Matrix m = new Matrix(); 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);
public static Bitmap getScaledBitmap(Bitmap b, int reqWidth, int reqHeight) { Matrix m = new Matrix(); 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); } //call this method like Bitmap bmp=getScaledBitmap(bm,50,50);
public static Bitmap getScaledBitmap(Bitmap b, int reqWidth, int reqHeight) { Matrix m = new Matrix(); 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); } //call this method like Bitmap bm400=getScaledBitmap(bm,500,500);
//Recreate the Matrix using the 2 Rects Matrix m = new Matrix() m.setRectToRect(mRectDst, mRectSrc, false); //Transform the coordinates back to the original plan float[] src = {x,y}; float[] dst = new float[2]; m.mapPoints(src, dst); myBitmap.getPixel(dst[0], dst[1]);
// Here previewRect is a rectangle which holds the camera's preview size, // pictureRect and nativeResRect hold the camera's picture size and its // native resolution, respectively. RectF previewRect = new RectF(0, 0, 480, 800), pictureRect = new RectF(0, 0, 1080, 1920), nativeResRect = new RectF(0, 0, 1952, 2592), resultRect = new RectF(0, 0, 480, 800); final Matrix scaleMatrix = new Matrix(); // create a matrix which scales coordinates of preview size rectangle into the // camera's native resolution. scaleMatrix.setRectToRect(previewRect, nativeResRect, Matrix.ScaleToFit.CENTER); // map the result rectangle to the new coordinates scaleMatrix.mapRect(resultRect); // create a matrix which scales coordinates of picture size rectangle into the // camera's native resolution. scaleMatrix.setRectToRect(pictureRect, nativeResRect, Matrix.ScaleToFit.CENTER); // invert it, so that we get the matrix which downscales the rectangle from // the native resolution to the actual picture size scaleMatrix.invert(scaleMatrix); // and map the result rectangle to the coordinates in the picture size rectangle scaleMatrix.mapRect(resultRect);
private void resetPreviewSize() { final boolean widthIsMax = mWidth > mHeight; final Camera.Size size = mCamera.getNativeCamera().getParameters().getPreviewSize(); final RectF rectDisplay = new RectF(); final RectF rectPreview = new RectF(); rectDisplay.set(0, 0, mWidth, mHeight); final int width = widthIsMax ? size.width : size.height; final int height = widthIsMax ? size.height : size.width; rectPreview.set(0, 0, width, height); final Matrix matrix = new Matrix(); matrix.setRectToRect(rectDisplay, rectPreview, Matrix.ScaleToFit.START); matrix.invert(matrix); matrix.mapRect(rectPreview); mCameraView.getLayoutParams().height = (int) (rectPreview.bottom); mCameraView.getLayoutParams().width = (int) (rectPreview.right); mCameraView.requestLayout(); }
private void setupMatrix(int width, int height, int degrees, boolean isHorizontal) { Log.d(TAG, "setupMatrix for " + degrees + " degrees"); Matrix matrix = new Matrix(); //The video will be streched if the aspect ratio is in 1,5(recording at 480) RectF src; if (isHorizontal) //In my case, I changed this line, cause with my onMeasure() and onLayout() methods my container view is already rotated and scaled, so I need to sent the inverted params to the src. src = new RectF(0, 0,mThumbnailContainer.getmWidth(), mThumbnailContainer.getmHeight()); else src = new RectF(0, 0, mThumbnailContainer.getmWidth(),mThumbnailContainer.getmHeight()); RectF dst = new RectF(0, 0, width, height); RectF screen = new RectF(dst); Log.d(TAG, "Matrix: " + width + "x" + height); Log.d(TAG, "Matrix: " + mThumbnailContainer.getmWidth() + "x" + mThumbnailContainer.getmHeight()); matrix.postRotate(degrees, screen.centerX(), screen.centerY()); matrix.mapRect(dst); matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER); matrix.mapRect(src); matrix.setRectToRect(screen, src, Matrix.ScaleToFit.FILL); matrix.postRotate(degrees, screen.centerX(), screen.centerY()); mVideoView.setTransform(matrix); }
final RectF src = new RectF(0, 0, bm.getWidth(), bm.getHeight()); final RectF dst = new RectF(0, 0, dstWidth - dx, dstHeight - dy); scaleToFit.setRectToRect(src, dst, ScaleToFit.CENTER);
private void transformImage (int width, int height) { if(mPreviewSize == null || mTextureView == null) { return; } Matrix matrix = new Matrix(); int rotation = getWindowManager().getDefaultDisplay().getRotation(); RectF textureRectF = new RectF(0,0,width,height); RectF previewRectF = new RectF(0,0,mPreviewSize.getHeight(),mPreviewSize.getWidth()); float centerX = textureRectF.centerX(); float centery = textureRectF.centerY(); if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) {} else if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) { previewRectF.offset(centerX - previewRectF.centerX(),centery-previewRectF.centerY()); matrix.setRectToRect(textureRectF,previewRectF,Matrix.ScaleToFit.FILL); float scale = Math.max((float)width / mPreviewSize.getWidth(),(float)height/ mPreviewSize.getHeight()); matrix.postScale(scale,scale,centerX,centery); matrix.postRotate(90*(rotation-2),centerX,centery); mTextureView.setTransform(matrix ); }
private void transformImage(int width, int height) { if (mPreviewSize == null || mTextureView == null) { return; } Matrix matrix = new Matrix(); int rotation = getWindowManager().getDefaultDisplay().getRotation(); RectF textureRectF = new RectF(0, 0, width, height); RectF previewRectF = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); float centerX = textureRectF.centerX(); float centerY = textureRectF.centerY(); if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) { previewRectF.offset(centerX - previewRectF.centerX(), centerY - previewRectF.centerY()); matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL); float scale = Math.max((float) width / mPreviewSize.getWidth(), (float) height / mPreviewSize.getHeight()); matrix.postScale(scale, scale, centerX, centerY); matrix.postRotate(90 * (rotation - 2), centerX, centerY); } mTextureView.setTransform(matrix); }
class V extends ImageView { public V(Context context) { super(context); setScaleType(ScaleType.MATRIX); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Drawable d = getDrawable(); if (d != null) { Matrix matrix = new Matrix(); RectF src = new RectF(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); RectF dst = new RectF(0, 0, w, h); matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER); float[] points = { 0, d.getIntrinsicHeight() }; matrix.mapPoints(points); matrix.postTranslate(0, h - points[1]); setImageMatrix(matrix); } } }