【问题标题】:UnsafeRawPointer to UnsafeMutableRawPointerUnsafeRawPointer 到 UnsafeMutableRawPointer
【发布时间】:2021-07-15 16:15:16
【问题描述】:

我正在创建一个封装Google Ml Kit 用于条形码扫描的 Flutter 插件。

现在我正在尝试将 this piece of code 转换为 swift 以扫描来自 Dart 端的字节数组,以便我可以使用 barcode scanner 功能对其进行处理。

This is my current implementation. 我收到以下错误:

9:101: error: cannot convert value of type 'UnsafeRawPointer' to expected argument type
'UnsafeMutableRawPointer'
            let pixelBuffer = createPixelBufferFromBytes(width: width!, height:height!, data:  data.bytes,
            bytes: planes![0]["bytes"] as! Int)

所以我想我必须将 UnsafeRawPointer 转换为 UnsafeMutableRawPointer。

那么,我怎样才能安全地进行这种转换呢?

【问题讨论】:

  • 您找到解决方案了吗?
  • 不,@SkandarMunir,我已经放弃了这个项目。
  • 哦,不要放弃它。我找到了解决方案并实施了它。效果很好。让我分享示例代码。

标签: ios flutter barcode-scanner firebase-mlkit


【解决方案1】:

我知道你已经实现了通道调用,直到将字节和平面转换为 UIImage。但我注意到在以下方面没有做太多工作:

在 Swift 中通过 Flutter Image Stream 重新生成图像。

因此,如果有人遇到相同的问题,我将在 SWIFT End 添加所有步骤。

第 1 步: 在 Swift 中通过 Channle 调用接收参数

guard let args = call.arguments else {
                result("iOS could not recognize flutter arguments in method: (yourFuncName)")
                return
            }

第 2 步:从字典中提取 4 图像所需的重要值 再生。

let params: [String : Any] = args as! [String : Any] 

let imageData: [String : Any] = params["imageData"] as! [String : Any]

第 3 步:将 imageData 作为 NSDictionary 传递给 UIImageFromImageData

 guard let image = UIImageFromImageData(imageData: imageData as NSDictionary ) else { return }

UIImageFromImageData:

  func UIImageFromImageData(imageData: NSDictionary) -> UIImage? {

            if(imageData.count > 0)
            {
            return bytesToImage(imageData: imageData)!;
            }else
            {
                return nil
            }
    }

bytesToImage

 func bytesToImage(imageData: [String : Any]) -> UIImage?{
        
          let imgD: FlutterStandardTypedData? = imageData["bytes"] as? FlutterStandardTypedData
        let imageBytes : Data
        imageBytes = imgD!.data
            
           let metadata: [String : Any] = imageData["metadata"] as! [String : Any]
    
           let planeData = metadata["planeData"] as! Array<Any>
           let planeCount = planeData.count
    
           let width = metadata["width"] as! Int
           let height = metadata["height"] as! Int
    
    
           let rawFormat = metadata["imageFormat"] as! NSNumber
            
        let pxBuffer: CVPixelBuffer?;

            if(planeCount==0)
            {
                //Throw Exception planes should be greated than Zero
                return nil
            }
            else if (planeCount==1)
            {
                let plane: [String : Any] = planeData[0] as! [String : Any]
                let bytesPerRow = plane["bytesPerRow"] as! Int
                let bytesData : UnsafeMutableRawBufferPointer
                let nsData = imageBytes as NSData
                let rawPtr = nsData.bytes
                let b:UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating:rawPtr)
                
                pxBuffer =  bytesToPixelBuffer(width: width, height: height, format: rawFormat, baseAddress: b, bytesPerRow: bytesPerRow)
                if pxBuffer != nil {
                    return UIImage(cgImage: pxBufferToCGImage(pixelBuffer: pxBuffer!)!)
                }
else
{
return
}
                
            }
            else
            {
              pxBuffer = planarBytesToPixelBuffer()
            }
        
        return UIImage()
    }

bytesToPixelBuffer

 func bytesToPixelBuffer(width: Int, height: Int, format: FourCharCode,baseAddress : UnsafeMutableRawPointer, bytesPerRow: Int) -> CVBuffer?{
        var dstPixelBuffer: CVBuffer?
        // Format is very Important use Same Format to regenerate buffer as you were using in Flutter Side I would recommend 1111970369 Raw format.

         CVPixelBufferCreateWithBytes(kCFAllocatorDefault, width, height, format, baseAddress, bytesPerRow,
                                                nil, nil, nil, &dstPixelBuffer)
        return dstPixelBuffer ?? nil

    }

【讨论】:

  • 您的答案无需重复。如果您想更改之前的答案,则应该对其进行编辑。
猜你喜欢
  • 2017-03-30
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多