简体   繁体   English

Xamarin.ios:从Objective-C到C#

[英]Xamarin.ios: Objective-C to C#

I am porting an application from iOS to a Xamarin project. 我正在将应用程序从iOS移植到Xamarin项目。 The Objective-C class is working but i don't really know how to write the C# version of it. Objective-C类正在工作,但我真的不知道如何编写它的C#版本。 If someone sees something wrong i would be grateful. 如果有人看到错了,我将不胜感激。

- (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);

.................
}

This is the part of my code where i try to port it for Xamarin platform: 这是我的代码的一部分,我尝试将其移植到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);

..............
}

There were a number of problems with the conversion of this class to c#. 此类到c#的转换存在许多问题。 One thing that i was doing was setting the wrong PixelFormatType to AVCaptureVideoDataOutput, so the pixelBuffer was the wrong. 我正在做的一件事是将错误的PixelFormatType设置为AVCaptureVideoDataOutput,因此pixelBuffer是错误的。 And just after getting resLength the pixelBuffer must be unlocked. 在获得resLength之后,必须将pixelBuffer解锁。 The working class is: 工人阶级是:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM