วิธีหมุนบิตแมป 90 องศา


123

มีคำสั่งใน android canvas.drawBitmap(visiblePage, 0, 0, paint);

พอเพิ่มcanvas.rotate(90)ก็ไม่มีผล แต่ถ้าเขียน

canvas.rotate(90)
canvas.drawBitmap(visiblePage, 0, 0, paint);

ฉันไม่ได้วาดบิตแมป แล้วฉันทำอะไรไม่ถูก?


ฉันตอบไว้ที่นี่: stackoverflow.com/questions/8608734/…
EyalBellisha

คำตอบ:


254

คุณยังสามารถลองใช้อันนี้

Matrix matrix = new Matrix();

matrix.postRotate(90);

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg, width, height, true);

Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

จากนั้นคุณสามารถใช้ภาพหมุนเพื่อตั้งค่าในมุมมองภาพของคุณผ่าน

imageView.setImageBitmap(rotatedBitmap);

1
ฉันคิดว่าสำหรับ scaledBitmap ที่คุณต้องการ (bitmapOrg, width, height, true)
Jameo

2
นำเข้าเมทริกซ์ใด android.graphics หรือ android.opengl?
Poutrathor

6
นำเข้า android.graphics
kirtan403

4
สิ่งนี้ใช้หน่วยความจำมาก สำหรับบิตแมปขนาดใหญ่อาจสร้างปัญหาได้เนื่องจากบิตแมปหลายสำเนาในหน่วยความจำ
Moritz ทั้ง

1
ในกรณีที่คุณไม่ต้องการบิตแมปดั้งเดิมให้โทรแจ้งbitmap.recycle()ให้แน่ใจ
Nick Bedford

174
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

ในการรับ Bitmap จากทรัพยากร:

Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);

1
ฉันใหม่กับ Android แค่สงสัยว่าถ้าฉันทำ Bitmap newBitmap = RotateBitmap (oldBitmap, 90) 'บิตแมปที่ถอดรหัส' ของฉันมีหน่วยความจำสองบล็อก (สำหรับเก่าและใหม่) หรือพวกเขาอ้างถึงหน่วยความจำเดียวกัน แต่อย่างใดอย่างหนึ่งไม่มีการหมุนส่วนอื่น ๆ มีการหมุน ? .... ข้อกังวลของฉันคือถ้าฉันถอดรหัส R.drawable.picture เป็น oldBitmap ถ้าสมมติว่าใช้หน่วยความจำ 2 MB (Heap ฉันเดา?) newBitmap จะใช้หน่วยความจำเพิ่มอีก 2 MB (เช่น 2 + 2 = รวม 4MB)? หรือ newBitmap จะอ้างถึง oldBitmap เท่านั้น (จึงไม่จำเป็นต้องใช้ 2MB เพิ่มเติม)? ......... ฉันต้องการหลีกเลี่ยงข้อผิดพลาด outOfMemory โดยเสียค่าใช้จ่าย!
Shishir Gupta

4
@ShishirGupta ไม่ได้ทดสอบ แต่โดย Android docs:If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.
Arvis

1
@Arvis เฮ้อาร์วิสฉันลองทำตามคำแนะนำของคุณแล้วมันใช้ได้กับการวางแนว แต่ตอนนี้ฉันได้ภาพที่มีแนวตั้งที่มีขนาดเล็กกว่ามาก ความคิดใด ๆ ?
Doug Ray

44

นามสกุลสั้นสำหรับKotlin

fun Bitmap.rotate(degrees: Float): Bitmap {
    val matrix = Matrix().apply { postRotate(degrees) }
    return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}

และการใช้งาน:

val rotatedBitmap = bitmap.rotate(90F) // value must be float

13

ด้านล่างนี้คือรหัสสำหรับหมุนหรือปรับขนาดรูปภาพของคุณใน Android

public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);

        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
}

คุณสามารถตรวจสอบรายละเอียดได้ที่ลิงค์นี้: http://www.anddev.org/resize_and_rotate_image_-_example-t621.html


6

ตามค่าเริ่มต้นจุดหมุนคือจุด (0,0) ของ Canvas และฉันเดาว่าคุณอาจต้องการหมุนรอบจุดศูนย์กลาง ฉันทำอย่างนั้น:

protected void renderImage(Canvas canvas)
{
    Rect dest,drawRect ;

    drawRect = new Rect(0,0, mImage.getWidth(), mImage.getHeight());
    dest = new Rect((int) (canvas.getWidth() / 2 - mImage.getWidth() * mImageResize / 2), // left
                    (int) (canvas.getHeight()/ 2 - mImage.getHeight()* mImageResize / 2), // top
                    (int) (canvas.getWidth() / 2 + mImage.getWidth() * mImageResize / 2), //right
                    (int) (canvas.getWidth() / 2 + mImage.getHeight()* mImageResize / 2));// bottom

    if(!mRotate) {
        canvas.drawBitmap(mImage, drawRect, dest, null);
    } else {
        canvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated.
        canvas.rotate(90,canvas.getWidth() / 2, canvas.getHeight()/ 2);
        canvas.drawBitmap(mImage, drawRect, dest, null);
        canvas.restore();
    }
}

4

ฉันจะลดความซับซ้อนของฟังก์ชันส่วนขยาย Kotlinของcomm1x ให้ง่ายขึ้น:

fun Bitmap.rotate(degrees: Float) =
    Bitmap.createBitmap(this, 0, 0, width, height, Matrix().apply { postRotate(degrees) }, true)

4

ใช้createBitmap()วิธีJava คุณสามารถส่งผ่านองศา

Bitmap bInput /*your input bitmap*/, bOutput;
float degrees = 45; //rotation degree
Matrix matrix = new Matrix();
matrix.setRotate(degrees);
bOutput = Bitmap.createBitmap(bInput, 0, 0, bInput.getWidth(), bInput.getHeight(), matrix, true);

1

หากคุณหมุนบิตแมป 90 180270360 ก็ใช้ได้ แต่สำหรับผืนผ้าใบองศาอื่น ๆ จะวาดบิตแมปที่มีขนาดต่างกัน

ดังนั้นวิธีที่ดีที่สุดคือ

canvas.rotate(degree,rotateCenterPoint.x,rotateCenterPoint.y);  
canvas.drawBitmap(...);
canvas.rotate(-degree,rotateCenterPoint.x,rotateCenterPoint.y);//rotate back

0

ในกรณีที่เป้าหมายของคุณคือการหมุนภาพใน imageView หรือไฟล์คุณสามารถใช้ Exif เพื่อบรรลุเป้าหมายนั้นได้ ตอนนี้ไลบรารีการสนับสนุนมี: https://android-developers.googleblog.com/2016/12/introducing-the-exifinterface-support-library.html

ด้านล่างนี้คือการใช้งาน แต่เพื่อให้บรรลุเป้าหมายของคุณคุณต้องตรวจสอบเอกสาร API ของไลบรารีสำหรับสิ่งนั้น ฉันแค่อยากจะบอกใบ้ว่าการหมุนบิตแมปไม่ใช่วิธีที่ดีที่สุดเสมอไป

Uri uri; // the URI you've received from the other app
InputStream in;
try {
  in = getContentResolver().openInputStream(uri);
  ExifInterface exifInterface = new ExifInterface(in);
  // Now you can extract any Exif tag you want
  // Assuming the image is a JPEG or supported raw format
} catch (IOException e) {
  // Handle any errors
} finally {
  if (in != null) {
    try {
      in.close();
    } catch (IOException ignored) {}
  }
}

int rotation = 0;
int orientation = exifInterface.getAttributeInt(
    ExifInterface.TAG_ORIENTATION,
    ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
  case ExifInterface.ORIENTATION_ROTATE_90:
    rotation = 90;
    break;
  case ExifInterface.ORIENTATION_ROTATE_180:
    rotation = 180;
    break;
  case ExifInterface.ORIENTATION_ROTATE_270:
    rotation = 270;
    break;
}

เมืองขึ้น

รวบรวม "com.android.support:exifinterface:25.1.0"


0

เพียงระมัดระวังประเภท Bitmap จากการเรียกแพลตฟอร์ม Java เช่นจากcomm1x 's และGnzlt ' s คำตอบเพราะมันอาจกลับ null ฉันคิดว่ามันมีความยืดหยุ่นมากกว่าหากพารามิเตอร์สามารถเป็น Number ใดก็ได้และใช้ infix เพื่อให้อ่านง่ายขึ้นอยู่กับรูปแบบการเข้ารหัสของคุณ

infix fun Bitmap.rotate(degrees: Number): Bitmap? {
    return Bitmap.createBitmap(
        this,
        0,
        0,
        width,
        height,
        Matrix().apply { postRotate(degrees.toFloat()) },
        true
    )
}

วิธีใช้?

bitmap rotate 90
// or
bitmap.rotate(90)
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.