我正在将应用程序从 iOS 移植到 Xamarin 项目。 Objective-C 类正在运行,但我真的不知道如何编写它的 C# 版本。如果有人发现问题,我将不胜感激。
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer,0);
int pixelFormat = CVPixelBufferGetPixelFormatType(imageBuffer);
switch (pixelFormat)
{
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0);
width = bytesPerRow;
height = CVPixelBufferGetHeightOfPlane(imageBuffer,0);
break;
case kCVPixelFormatType_422YpCbCr8:
bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0);
width = CVPixelBufferGetWidth(imageBuffer);
height = CVPixelBufferGetHeight(imageBuffer);
int len = width*height;
int dstpos=1;
for (int i=0;i<len;i++)
{
baseAddress[i]=baseAddress[dstpos];
dstpos+=2;
}
break;
default:
break;
}
unsigned char *pResult=NULL;
int resLength = MWB_scanGrayscaleImage(baseAddress,width,height, &pResult);
.................
}
这是我尝试将其移植到 Xamarin 平台的代码部分:
public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
{
using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer)
{
pixelBuffer.Lock (0);
var baseAddress = pixelBuffer.BaseAddress;
int bytesPerRow = pixelBuffer.BytesPerRow;
int width = pixelBuffer.Width;
int height = pixelBuffer.Height;
//this is where i have to make a conversion because i need a byte[] from baseAddress
byte [] managedArray = new byte[width*height];
Marshal.Copy(baseAddress, managedArray, 0, width*height);
byte [] rawResult = new byte[3000];
// When it hits this line the app comes to a stop without any error message
// Declaration: public static extern int MWB_scanGrayscaleImage (byte[] gray, int width, int height, out byte[] result);
int resLength = BarcodeScannerClass.MWB_scanGrayscaleImage(managedArray,width,height,out rawResult);
..............
}
最佳答案
将此类转换为 C# 时出现了一些问题。我正在做的一件事是将错误的 PixelFormatType 设置为 AVCaptureVideoDataOutput,因此 pixelBuffer 是错误的。在获得 resLength 之后,必须解锁 pixelBuffer。 worker 类(Class)是:
public class OutputRecorder : AVCaptureVideoDataOutputSampleBufferDelegate
{
public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
{
try
{
using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer)
{
pixelBuffer.Lock (0);
CVPixelFormatType ft = pixelBuffer.PixelFormatType;
var baseAddress = pixelBuffer.BaseAddress;
int bytesPerRow = pixelBuffer.BytesPerRow;
int width = pixelBuffer.Width;
int height = pixelBuffer.Height;
byte [] managedArray = new byte[width * height];
Marshal.Copy(baseAddress, managedArray, 0, width * height);
byte [] rawResult = new byte[3000];
int resLength = BarcodeScannerClass.MWB_scanGrayscaleImage(managedArray,width,height,out rawResult);
pixelBuffer.Unlock (0);
关于c# - Xamarin.ios:从 Objective-C 到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20105492/