কীভাবে একটি বিটম্যাপ 90 ডিগ্রি ঘোরান


123

অ্যান্ড্রয়েডে একটি বিবৃতি আছে 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
আমি মনে করি আপনি স্কেলডবিটম্যাপের জন্য চান (বিটম্যাপ ওর্গ, প্রস্থ, উচ্চতা, সত্য)
জামেও

2
কোন ম্যাট্রিক্স আমদানি? android.ographicics বা android.opengl?
পোউট্রাথর


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
আমি অ্যান্ড্রয়েড নিয়ে নতুন। আমি ভাবছি যদি আমি বিটম্যাপ নিউ বিটম্যাপ = রোটেট বিটম্যাপ (পুরাতন বিটম্যাপ, 90) করি তবে আমার 'ডিকোডেড বিটম্যাপ' এর দুটি স্মৃতি ব্লক রয়েছে (পুরানো এবং নতুনের জন্য) বা তারা একই মেমরিটির উল্লেখ করছে তবে একটির কোনও আবর্তন নেই, অন্যটি ঘূর্ণন রয়েছে ? .... আমার উদ্বেগটি হ'ল, আমি যদি আর.ড্রেবলযোগ্য.পিকচারটি পুরাতন বিটম্যাপে ডিকোড করি, যদি মনে হয় এটি 2 এমবি মেমরি গ্রহণ করে (হিপ আমি অনুমান করি?), নতুন বিটম্যাপ অতিরিক্ত 2 এমবি মেমরি গ্রহণ করবে (যেমন 2 + 2 = মোট 4 এমবি)? অথবা নতুন বিটম্যাপটি কি কেবল পুরাতন বিটম্যাপকে উল্লেখ করবে (এবং এর ফলে কোনও অতিরিক্ত 2 এমবি লাগবে না)? ......... আমি কোনও মূল্য ব্যতীত স্মৃতি ত্রুটি এড়াতে চাই!
শিশির গুপ্ত

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
@ আরভিস আরে আরভিস আমি আপনার পরামর্শটি চেষ্টা করেছি এবং এটি ওরিয়েন্টেশনের জন্য কাজ করে তবে এখন আমি অনেক ছোট প্রতিকৃতি কেন্দ্রিক চিত্র পাচ্ছি। কোন ধারনা ?
ডগ রে

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

অ্যান্ড্রয়েডে আপনার চিত্র ঘোরানো বা পুনরায় আকার দেওয়ার কোডের নীচে is

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

আমি কম ১ এক্স এর কোটলিন এক্সটেনশন ফাংশন আরও সহজ করব:

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 90 270 360 ঠিক আছে তবে অন্যান্য ডিগ্রির জন্য ক্যানভাস বিভিন্ন আকারের সাথে বিটম্যাপ আঁকবে।

সুতরাং, সবচেয়ে ভাল উপায়

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

0

যদি আপনার লক্ষ্যটি কোনও চিত্রভিউ বা ফাইলে একটি ঘোরানো চিত্র থাকে তবে আপনি এটি অর্জনের জন্য এক্সিফটি ব্যবহার করতে পারেন। সমর্থন লাইব্রেরি এখন এগুলি সরবরাহ করে: 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:25.1.0" সংকলন করুন


0

জাভা প্ল্যাটফর্ম কল থেকে বিটম্যাপ প্রকারের মতো কম 1 এক্স এর উত্তর এবং গঞ্জল্টের উত্তরগুলি থেকে কেবল সাবধান হন , কারণ এটি শূন্য হতে পারে। আমি মনে করি এটি আরও নমনীয় যদি প্যারামিটারটি কোনও নম্বর হতে পারে এবং পাঠযোগ্যতার জন্য ইনফিক্স ব্যবহার করতে পারে তবে আপনার কোডিং শৈলীর উপর নির্ভর করে।

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.