2

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 :

  1. I tried not to compress the bitmap but no use.
  2. I tried to set the width and height of the bitmap according to screen size but didn't get a solid lead.
  3. 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.

San
  • 2,078
  • 1
  • 24
  • 42
  • there's a `createScaledBitmap` method directly in the Bitmap class: https://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap(android.graphics.Bitmap, int, int, boolean) You should use it. Alternatively there's also `mapRect` and `scale` methods in Matrix that you could use direclty when calling `createBitmap` method with a matrix – Budius Dec 30 '15 at 15:49
  • Thanks, I think this mapRect might be of some help. Will check it out. – San Dec 31 '15 at 05:34
  • I tried to search for it but couldn't find a good example, can you point me to an example ? – San Jan 04 '16 at 08:18
  • hi. I'm very sorry but I pointed you to the wrong method. The method you probably want to look into is: `setRectToRect`. This method will set the values of the matrix to map from one rectangle to the other. – Budius Jan 04 '16 at 09:27
  • Ok, will check and let you know. – San Jan 04 '16 at 09:29
  • I tried that way but no change at all, I have updated the question with the code with which I tried. Please take a look and let me know what I'm doing wrong. – San Jan 04 '16 at 10:17
  • 1
    Every 'set___' method in matrix overrides all the values in the matrix. So, when you call 'setRotate' you're clearing the values of 'setRectToRect' you should use 'preRotate'' or 'postRotate' depending what how you're processing the math. – Budius Jan 04 '16 at 11:31
  • Also remember that 'setRectTpRect' already is a type of scale. You're doing another scale after so one will be influencing the other. – Budius Jan 04 '16 at 11:33
  • But I need to rotate the image and only after rotating the image, I need to scale it. If one overrides the other then what would be the correct way to achieve rotate and applying rect ? – San Jan 04 '16 at 11:37
  • 1
    I guess than it will be the `preRotate`. I suggest a good read on Matrix docs: https://developer.android.com/reference/android/graphics/Matrix.html and as a last comment. `Matrix` are very powerful but also pretty complex. When I worked with them, I would write 1 operation, and then execute the code to test that 1 behavior is correct, before going to the next operation on the matrix. And on every step I had to actually make drawings on paper to wrap my head on what was happening. Best of luck. – Budius Jan 04 '16 at 11:40
  • Thanks for your help mate. Much appreciated. – San Jan 04 '16 at 11:41

0 Answers0