আউটপুট ট্রান্সফর্ম করুন

CameraX ব্যবহারের ক্ষেত্রে আউটপুট দুই ধরনের হয়: বাফার ও ট্রান্সফর্মেশন তথ্য। বাফার হল বাইট অ্যারে এবং ট্রান্সফর্মেশন তথ্য হল ব্যবহারকারীকে দেখানোর আগে বাফার কীভাবে ক্রপ ও রোটেট করা উচিত। কীভাবে ট্রান্সফর্মেশন প্রয়োগ করতে হবে তা বাফারের ফর্ম্যাটের উপর নির্ভর করে।

ImageCapture

ImageCapture ব্যবহারের ক্ষেত্রে, ডিস্কে সেভ করার আগে ক্রপ রেক্ট বাফার প্রয়োগ করা হয় এবং Exif ডেটাতে রোটেশন সেভ করা হয়। অ্যাপের জন্য অতিরিক্ত কোনও অ্যাকশন নেওয়ার প্রয়োজন নেই।

প্রিভিউ

Preview ব্যবহারের ক্ষেত্রে, আপনি SurfaceRequest.setTransformationInfoListener()-এ কল করে ট্রান্সফর্মেশন সংক্রান্ত তথ্য পেতে পারেন। ট্রান্সফর্মেশন আপডেট করা হলে, কলার একটি নতুন SurfaceRequest.TransformationInfo অবজেক্ট পায়।

ট্রান্সফর্মেশন সংক্রান্ত তথ্য কীভাবে প্রয়োগ করতে হবে তা Surface-এর সোর্সের উপর নির্ভর করে এবং সাধারণত তা সহজ নয়। যদি লক্ষ্যটি কেবল প্রিভিউ প্রদর্শন করা হয়, তাহলে PreviewView ব্যবহার করুন। PreviewView হল একটি কাস্টম ভিউ যা অটোমেটিক ট্রান্সফর্মেশন ম্যানেজ করে। উন্নত ব্যবহারের জন্য, যখন আপনাকে প্রিভিউ স্ট্রিম এডিট করতে হবে, যেমন OpenGL-এর সাথে, CameraX কোর টেস্ট অ্যাপ-এ কোড স্যাম্পেল দেখুন।

স্থানাঙ্ক পরিবর্তন করো

আরেকটি সাধারণ কাজ হল বাফারের পরিবর্তে কোঅর্ডিনেট নিয়ে কাজ করা, যেমন প্রিভিউতে শনাক্ত করা মুখের চারপাশে একটি বক্স আঁকা। এই ধরনের ক্ষেত্রে, আপনাকে শনাক্ত করা মুখের কোঅর্ডিনেট ইমেজ বিশ্লেষণ থেকে প্রিভিউতে পরিবর্তন করতে হবে।

নিম্নলিখিত কোড স্নিপেট একটি ম্যাট্রিক্স তৈরি করে যা ইমেজ বিশ্লেষণ স্থানাঙ্ক থেকে PreviewView স্থানাঙ্কে ম্যাপ করে। Matrix-এর সাহায্যে (x, y) কোঅর্ডিনেট ট্রান্সফর্ম করতে, Matrix.mapPoints() দেখুন।

Kotlin

fun getCorrectionMatrix(imageProxy: ImageProxy, previewView: PreviewView) : Matrix {
   val cropRect = imageProxy.cropRect
   val rotationDegrees = imageProxy.imageInfo.rotationDegrees
   val matrix = Matrix()

   // A float array of the source vertices (crop rect) in clockwise order.
   val source = floatArrayOf(
       cropRect.left.toFloat(),
       cropRect.top.toFloat(),
       cropRect.right.toFloat(),
       cropRect.top.toFloat(),
       cropRect.right.toFloat(),
       cropRect.bottom.toFloat(),
       cropRect.left.toFloat(),
       cropRect.bottom.toFloat()
   )

   // A float array of the destination vertices in clockwise order.
   val destination = floatArrayOf(
       0f,
       0f,
       previewView.width.toFloat(),
       0f,
       previewView.width.toFloat(),
       previewView.height.toFloat(),
       0f,
       previewView.height.toFloat()
   )

   // The destination vertexes need to be shifted based on rotation degrees. The
   // rotation degree represents the clockwise rotation needed to correct the image.

   // Each vertex is represented by 2 float numbers in the vertices array.
   val vertexSize = 2
   // The destination needs to be shifted 1 vertex for every 90° rotation.
   val shiftOffset = rotationDegrees / 90 * vertexSize;
   val tempArray = destination.clone()
   for (toIndex in source.indices) {
       val fromIndex = (toIndex + shiftOffset) % source.size
       destination[toIndex] = tempArray[fromIndex]
   }
   matrix.setPolyToPoly(source, 0, destination, 0, 4)
   return matrix
}

জাভা

Matrix getMappingMatrix(ImageProxy imageProxy, PreviewView previewView) {
   Rect cropRect = imageProxy.getCropRect();
   int rotationDegrees = imageProxy.getImageInfo().getRotationDegrees();
   Matrix matrix = new Matrix();

   // A float array of the source vertices (crop rect) in clockwise order.
   float[] source = {
       cropRect.left,
       cropRect.top,
       cropRect.right,
       cropRect.top,
       cropRect.right,
       cropRect.bottom,
       cropRect.left,
       cropRect.bottom
   };

   // A float array of the destination vertices in clockwise order.
   float[] destination = {
       0f,
       0f,
       previewView.getWidth(),
       0f,
       previewView.getWidth(),
       previewView.getHeight(),
       0f,
       previewView.getHeight()
   };

   // The destination vertexes need to be shifted based on rotation degrees.
   // The rotation degree represents the clockwise rotation needed to correct
   // the image.

   // Each vertex is represented by 2 float numbers in the vertices array.
   int vertexSize = 2;
   // The destination needs to be shifted 1 vertex for every 90° rotation.
   int shiftOffset = rotationDegrees / 90 * vertexSize;
   float[] tempArray = destination.clone();
   for (int toIndex = 0; toIndex < source.length; toIndex++) {
       int fromIndex = (toIndex + shiftOffset) % source.length;
       destination[toIndex] = tempArray[fromIndex];
   }
   matrix.setPolyToPoly(source, 0, destination, 0, 4);
   return matrix;
}