CameraX ব্যবহারের আউটপুট দ্বিমুখী: বাফার এবং ট্রান্সফরমেশন তথ্য। বাফারটি একটি বাইট অ্যারে এবং ট্রান্সফরমেশন তথ্যটি হলো, চূড়ান্ত ব্যবহারকারীদের দেখানোর আগে বাফারটিকে কীভাবে ক্রপ ও রোটেট করা উচিত। ট্রান্সফরমেশনটি কীভাবে প্রয়োগ করা হবে তা বাফারের ফরম্যাটের উপর নির্ভর করে।
ইমেজক্যাপচার
ImageCapture ব্যবহারের ক্ষেত্রে, ডিস্কে সংরক্ষণ করার আগে ক্রপ রেক্ট বাফার প্রয়োগ করা হয় এবং রোটেশনটি এক্সিফ ডেটাতে সংরক্ষিত হয়। অ্যাপের পক্ষ থেকে কোনো অতিরিক্ত পদক্ষেপের প্রয়োজন নেই।
প্রিভিউ
Preview ব্যবহারের ক্ষেত্রে, আপনি SurfaceRequest.setTransformationInfoListener() কল করে ট্রান্সফরমেশন তথ্য পেতে পারেন। প্রতিবার ট্রান্সফরমেশন আপডেট হলে, কলার একটি নতুন SurfaceRequest.TransformationInfo অবজেক্ট পায়।
ট্রান্সফরমেশন তথ্য কীভাবে প্রয়োগ করতে হবে তা Surface উৎসের উপর নির্ভর করে এবং এটি সাধারণত বেশ জটিল। যদি উদ্দেশ্য শুধু প্রিভিউ প্রদর্শন করা হয়, তাহলে PreviewView ব্যবহার করুন। PreviewView হলো একটি কাস্টম ভিউ যা স্বয়ংক্রিয়ভাবে ট্রান্সফরমেশন পরিচালনা করে। উন্নত ব্যবহারের জন্য, যখন আপনার প্রিভিউ স্ট্রিম সম্পাদনা করার প্রয়োজন হয়, যেমন OpenGL-এর ক্ষেত্রে, CameraX কোর টেস্ট অ্যাপের কোড স্যাম্পলটি দেখুন।
স্থানাঙ্ক রূপান্তর করুন
আরেকটি সাধারণ কাজ হলো বাফারের পরিবর্তে স্থানাঙ্ক নিয়ে কাজ করা, যেমন প্রিভিউতে শনাক্ত করা মুখের চারপাশে একটি বক্স আঁকা। এই ধরনের ক্ষেত্রে, ইমেজ অ্যানালাইসিস থেকে প্রিভিউতে শনাক্ত করা মুখের স্থানাঙ্ক রূপান্তর করতে হয়।
নিম্নলিখিত কোড স্নিপেটটি একটি ম্যাট্রিক্স তৈরি করে যা ইমেজ অ্যানালাইসিস কোঅর্ডিনেট থেকে PreviewView কোঅর্ডিনেটে ম্যাপ করে। একটি Matrix ব্যবহার করে (x, y) কোঅর্ডিনেট রূপান্তর করতে, Matrix.mapPoints() দেখুন।
কোটলিন
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; }