web-dev-qa-db-ja.com

android

サイズが320x480Bitmapがあり、さまざまなデバイス画面でストレッチする必要があるので、これを使用してみました。

Rect dstRect = new Rect();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(frameBuffer, null, dstRect, null);

それは機能し、画像は私が望んでいたように画面全体に表示されますが、画像はピクセル化されており、見栄えが悪くなっています。それから私は試しました:

float scaleWidth = (float) newWidth / width;
float scaleHeight = (float) newHeight / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(frameBuffer, 0, 0,
                width, height, matrix, true);
canvas.drawBitmap(resizedBitmap, 0, 0, null);

今回は完璧で、素晴らしくスムーズに見えますが、このコードはメインのゲームループに含まれている必要があり、反復ごとにBitmapsを作成すると非常に遅くなります。画像がピクセル化されず、高速に処理されるように、画像のサイズを変更するにはどうすればよいですか?

解決策を見つけました:

Paint paint = new Paint();
Paint.setFilterBitmap();
canvas.drawBitmap(bitmap, matrix, Paint);
15
user924941

ビットマップのサイズ変更:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

かなり自明です:元のビットマップオブジェクトとビットマップの必要な寸法を入力するだけで、このメソッドは新しくサイズ変更されたビットマップに戻ります!たぶん、それはあなたにとって便利です。

34
Piyush

上記のソリューションを使用してビットマップのサイズを変更しています。ただし、画像の一部が失われる結果になります。

これが私のコードです。

 BitmapFactory.Options bmFactoryOptions = new BitmapFactory.Options();
            bmFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bmFactoryOptions.inMutable = true;
            bmFactoryOptions.inSampleSize = 2;
            Bitmap originalCameraBitmap = BitmapFactory.decodeByteArray(pData, 0, pData.length, bmFactoryOptions);
            rotatedBitmap = getResizedBitmap(originalCameraBitmap, cameraPreviewLayout.getHeight(), cameraPreviewLayout.getWidth() - preSizePriviewHight(), (int) rotationDegrees);

 public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, int angle) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        DeliverItApplication.getInstance().setImageCaptured(true);
        return resizedBitmap;
    }

画像の高さと幅は次のとおりです。プレビューサーフェスサイズ:352:288サイズ変更前のビットマップ幅:320高さ:240 CameraPreviewLayout幅:1080高さ:1362サイズ変更されたビットマップ幅:1022高さ:1307

0
Shakeera Bettal