2

我正在尝试从一组图像创建视频,这些图像可能是纵向或横向的。我希望最终视频是横向的,因此我将纵向图像的大小重新调整为横向图像的尺寸。显然,图像质量很差,而且会被拉伸。我尝试在重新缩放后将位图旋转到横向模式,但无法达到我的要求。我想要的只是一组具有相同尺寸的图像,如果在重新调整大小时肖像图像的两侧有一些黑色空间,我绝对没有问题。

到目前为止我所尝试的:

                        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;
    }

其他选择:

  1. 我试图不压缩位图但没有用。
  2. 我试图根据屏幕尺寸设置位图的宽度和高度,但没有得到可靠的引导。
  3. 我试图重新分配纵向图像的宽度和高度,以便它们的尺寸将是横向图像的尺寸。

关于如何将肖像图像重新缩放到风景或风景尺寸但肖像图像在中心有或没有任何两侧的任何黑色空间的任何想法?任何帮助或见解将不胜感激!

PS:

我正在使用这些位图图像来创建视频,因此我不希望将其放入 IMAGEVIEW中。我的目标只是将纵向位图转换为具有相同尺寸但不拉伸的横向位图。

编辑 :

根据评论部分Budius的建议,我尝试使用setRectToRect以下方法,但这并没有给我带来太大的改变。

            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);

setRectToRect之后应用setRotate并且postTranslate不旋转图像,但在纵向模式下显示相同的图像。我希望所有图像都处于横向模式,如果我应用旋转,那么我的结果图像是这样。我只是不希望它被拉伸。我希望该图像看起来像普通的风景图像或像这样。任何帮助将非常感激。

4

0 回答 0