I am trying to create a video from a set of images, those images might be of Portrait or Landscape. I want the final video to be of Landscape so I am re-sizing the portrait image to the dimensions of a landscape image. Obviously, the image quality is very poor and it gets stretched. I tried rotating the bitmap to landscape mode after re-scaling but not able to achieve what I require. All I want is a set of images with the same dimensions, and I have absolutely no problem if there is some black space on the either side of the portrait image while re-sizing.
What I have tried so far :
Bitmap bmp = BitmapFactory.decodeFile(created_folder + File.separator + "pic00" + (k + 1) + ".jpg");
Matrix matrix = new Matrix();
if (image_orientaion == 6) {
matrix.postRotate(90);
} else if (image_orientaion == 3) {
matrix.postRotate(180);
} else if (image_orientaion == 8) {
matrix.postRotate(270);
}
//bmp = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, (dm.widthPixels * bmp.getHeight() / bmp.getWidth()), matrix, true);
Log.e("The Iriginal Heihgt and Width is ","Width " + bmp.getWidth() + " Height " + bmp.getHeight());
if (bmp.getWidth() < bmp.getHeight()) {
bmp = scaleBitmap(bmp,bmp.getWidth(),bmp.getHeight());
Log.e("The Height and Wiedth after scalimg is "," Width : " + bmp.getWidth() + "Height : " + bmp.getHeight());
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getHeight(), bmp.getWidth(), matrix, true);
} else {
bmp = RescalingBitmap(bmp);
Log.e("The Height and Wiedth after scalimg is "," Width : " + bmp.getWidth() + "Height : " + bmp.getHeight());
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
// Log.e("The Bitmap height and width is ","Height is " + bmp.getHeight() + " Width is " + bmp.getWidth());
FileOutputStream fOut;
try {
fOut = new FileOutputStream(created_folder + File.separator + "pic00" + (k + 1) + ".jpg");
bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
bmp.recycle();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
float originalWidth = bitmap.getWidth();
float originalHeight = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Matrix m = new Matrix();
float scalex = wantedWidth/originalWidth;
float scaley = wantedHeight/originalHeight;
float xTranslation = 0.0f, yTranslation = (wantedHeight - originalHeight * scaley)/2.0f;
m.postTranslate(xTranslation, yTranslation);
m.preScale(scalex, scaley);
// m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(bitmap, m, paint);
return output;
}
Other alternatives :
- I tried not to compress the bitmap but no use.
- I tried to set the width and height of the bitmap according to screen size but didn't get a solid lead.
- I have tried to re-assign the width and height of the portrait image so that their dimension will be of a dimension of a landscape image.
Any idea on how to re-scale the portrait image to that of a landscape or that of a landscape dimension but with the portrait image in the center with or without any black spaces on either side ? Any help or insights will be much appreciated !
P.S :
I am using these bitmap images to create a video so I AM NOT LOOKING TO FIT THIS IN A IMAGEVIEW. Just converting the portrait bitmap to a landscape one with the same dimension but not stretched is my aim.
EDIT :
According to the suggestion of Budius from comments section, I tried to use setRectToRect
method as follows but that didn't bring me much change.
Bitmap bmp = decodeFile(new File(created_folder + File.separator + "pic00" + (k + 1) + ".jpg"));
Matrix matrix = new Matrix();
float scale = 1024/bmp.getWidth();
float xTranslation = 0.0f, yTranslation = (720 - bmp.getHeight() * scale)/2.0f;
RectF drawableRect = new RectF(0, 0, bmp.getWidth()-100,
bmp.getHeight()-100);
RectF viewRect = new RectF(0, 0, bmp.getWidth(),
bmp.getHeight());
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
matrix.setRotate(90, bmp.getWidth(), bmp.getHeight());
matrix.postTranslate(xTranslation, yTranslation);
matrix.preScale(scale, scale);
Applying setRectToRect
after setRotate
and postTranslate
doesn't rotate the image but shows the same image in portrait mode. I want all images to be in landscape mode, if I apply rotation then my resulting image is like this. I just dont want it to be stretched. I want that image to look like a normal landscape image or like this. Any help would be much appreciated.