c# - Xamarin.ios:从 Objective-C 到 C#

标签 c# ios objective-c xamarin.ios xamarin

我正在将应用程序从 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/

相关文章:

c# - 为什么 HttpClient.PostAsync 不会抛出失败?

c# - 如何在列表框末尾制作“更多”按钮并处理它?

ios - 在iOS8中获取联系人列表,导致应用崩溃

iphone - AVAudioPlayer时间显示

ios - Swift协议(protocol)问题,如何表达

c# - 我的正则表达式做错了什么?

c# - 当无法访问 Web 服务 URL 时,如何在 Windows Mobile 中显示 "cannot connect"对话框?

ios - 应用程序图标文字大小不合适

ios - 防止在检索远程文件时由于连接速度慢而导致应用程序崩溃

ios - BLE 设备应该在不扫描的情况下连接