2017-09-22 5 views
0

私はあなた自身の写真を選んで、それに対する境界線を選択してイメージとして保存することができる簡単なアプリケーションを構築しました。私がこれをしたのは、2つのImageViewを親ビューの上に重ねて追加することでした。この親ビューをイメージに変換して保存します。ただし、この方法では、結果の画像サイズはデバイスの画面サイズに依存します。デバイス画面が小さい場合は、小さな最終イメージが生成されます。Androidで固定サイズの画像ファイルを作成する方法は?

私が望むのは、デバイスに関係なく、常に500x800ピクセルのサイズのイメージファイルを作成することです。これを行う正しい方法は何ですか?

画面のプレビューは小さくてもかまいませんが、保存ボタンをクリックすると正確に500x800になる必要があります。

答えて

0

まず、によってビットマップに自分のイメージを変換:

Bitmap bitmapImage= BitmapFactory.decodeResource(getResources(), R.drawable.large_icon); 

それとも、あなたは画像のURIを持っているならば、ビットマップに変換し、これを使用します。

Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()); 

これのどちらがあなたを与えるだろうあなたのイメージのビットマップ。

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, imageHeight, true); 

次にファイルに変換:あなたのイメージは、このコードを使用するサイズを変更するには今すぐ

0

Jason Answerに追加すると、画像を拡大/縮小するための最良の方法が見つかりました。これを使用して画像を上下に拡大することができます。

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { 
    int sourceWidth = source.getWidth(); 
    int sourceHeight = source.getHeight(); 
    // Compute the scaling factors to fit the new height and width, respectively. 
    // To cover the final image, the final scaling will be the bigger 
    // of these two. 
    float xScale = (float) newWidth/sourceWidth; 
    float yScale = (float) newHeight/sourceHeight; 
    float scale = Math.max(xScale, yScale); 
    // Now get the size of the source bitmap when scaled 
    float scaledWidth = scale * sourceWidth; 
    float scaledHeight = scale * sourceHeight; 
    // Let's find out the upper left coordinates if the scaled bitmap 
    // should be centered in the new size give by the parameters 
    float left = (newWidth - scaledWidth)/2; 
    float top = (newHeight - scaledHeight)/2; 
    // The target rectangle for the new, scaled version of the source bitmap will now 
    // be 
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); 
    // Finally, we create a new bitmap of the specified size and draw our new, 
    // scaled bitmap onto it. 
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); 
    Canvas canvas = new Canvas(dest); 
    canvas.drawBitmap(source, null, targetRect, null); 
    return dest; 
} 
0

以下、カスタムの高さ&幅としてビットマップイメージを保存するためにコードの下にしてみてくださいビットマップのサイズを変更にあなたをできるようになりますコードのスニペットです。

public Bitmap getResizeBitmap(Bitmap bmp, int newHeight, int newWidth) { 

int width = bmp.getWidth(); 
int height = bmp.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 resizeBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false); 

return resizeBitmap; 
} 
関連する問題