बिटमैप को 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
मुझे लगता है कि आप जिस स्केलडिटमैप को चाहते हैं (बिटमैपऑर्ग, चौड़ाई, ऊंचाई, सच)
Jameo

2
कौन सा मैट्रिक्स आयात करें? android.graphics या android.opengl?
पौटरथोर

6
आयात android.graphics
कीर्तन403

4
यह बहुत सारी मेमोरी का उपयोग करता है। बड़े बिटमैप्स के लिए, यह मेमोरी में बिटमैप्स की कई प्रतियों के कारण समस्याएं पैदा कर सकता है।
मोरिट्ज़ दोनों

1
यदि आपको मूल बिटमैप की आवश्यकता नहीं है, तो bitmap.recycle()निश्चित होने के लिए कॉल करें ।
निक बेडफोर्ड

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 source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);

1
मैं Android के साथ नया हूँ। बस सोच रहा था कि क्या मैं बिटमैप न्यूबाइटमैप = रोटेटबिटमैप (पुराना बिटमैप, 90) करता हूं, क्या मेरे 'डिकोड्ड बिटमैप' में दो मेमोरी ब्लॉक हैं (पुराने और नए के लिए) या क्या वे एक ही मेमोरी का जिक्र कर रहे हैं, लेकिन कोई रोटेशन नहीं कर रहा है, दूसरे में रोटेशन नहीं है ? .... मेरी चिंता है, अगर मैं पुराने बिटमैप में R.drawable.picture को डिकोड करता हूं, अगर यह मान लें कि 2 एमबी मेमोरी (हीप मुझे लगता है?) है, तो क्या अतिरिक्त बिट एमबी 2 अतिरिक्त मेमोरी (यानी 2 या 2 =) लेगा? 4MB कुल)? या नया बिटमैप केवल पुराने बिटमैप को संदर्भित करेगा (और इस प्रकार अतिरिक्त 2 एमबी की आवश्यकता नहीं है)? ......... मैं हर कीमत पर OutOememory त्रुटि से बचना चाहता हूँ!
शिशिर गुप्ता

4
@ शिशिरगुप्ता ने परीक्षण नहीं किया, लेकिन एंड्रॉइड डॉक्स द्वारा: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.
अरविस

1
@Arvis अरे, मैंने आपके सुझाव की कोशिश की और यह अभिविन्यास के लिए काम करता है, हालांकि अब मुझे एक बहुत छोटा चित्र केंद्रित छवि मिल रही है। कोई विचार ?
डग रे

44

कोटलिन के लिए छोटा विस्तार

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) बिंदु है, और मेरा अनुमान है कि आप इसे केंद्र के चारों ओर घुमाना चाहते हैं। मैंने वह किया:

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

मैं आसान बनाने में होता comm1x के Kotlin विस्तार समारोह और भी अधिक:

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

4

जावा createBitmap()विधि का उपयोग करके आप डिग्री पास कर सकते हैं।

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 180 270 360 ठीक है, लेकिन अन्य डिग्री के लिए कैनवास अलग-अलग आकार के बिटमैप को आकर्षित करेगा।

तो, सबसे अच्छा तरीका है

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

0

यदि आपका लक्ष्य किसी इमेज व्यू या फ़ाइल में घुमाई गई छवि है, तो आप इसे प्राप्त करने के लिए Exif का उपयोग कर सकते हैं। अब समर्थन पुस्तकालय प्रदान करता है कि: https://android-developers.googleblog.com/2016/12/introducing-the-exifinterface-support-library.html

नीचे इसका उपयोग किया गया है, लेकिन अपने लक्ष्य को प्राप्त करने के लिए आपको उसके लिए लाइब्रेरी एपि दस्तावेज की जांच करनी होगी। मैं सिर्फ एक संकेत देना चाहता था कि बिटमैप को घुमाना हमेशा सबसे अच्छा तरीका नहीं है।

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

बस जावा प्लेटफॉर्म कॉल से बिटमैप के प्रकार से सावधान रहें जैसे कि कॉम 1 एक्स के जवाब और ग्नज़ेल के उत्तर, क्योंकि यह अशक्त हो सकता है। मुझे लगता है कि यह अधिक लचीला है अगर पैरामीटर कोई भी संख्या हो सकती है और पठनीयता के लिए 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)
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.