在Android

时间:2015-12-30 15:39:08

标签: android bitmap aspect-ratio rescale

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

到目前为止我尝试了什么:

                        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. 我尝试重新分配肖像图像的宽度和高度,使其尺寸为横向图像的尺寸。
  4. 关于如何将肖像图像重新缩放到横向或横向尺寸的图像,但是在中心有或没有任何黑色空间的肖像图像的任何想法?任何帮助或见解将不胜感激!

    P.S:

    我正在使用这些位图图像来创建一个视频,所以我不想在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);
    

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

0 个答案:

没有答案