react-native-vision-camera: š iOS barcode scanning is very slow (unusable in some cases)
Whatās happening?
On iOS the native APIās are performing very poorly (all devices that I have tested on). The time to scan a regular barcode is so slow and inaccurate compared to Android that uses MLKit. The QR scanning on iOS is fine, but the other types of barcodes (vertical lines, ie: upc/code/etc) I have tested have been almost unusable, what takes android 2 seconds to scan/verify will take iOS almost 10 times as long sometimes.
Some additional context - when I say scan/āverifyā we are waiting to see the same code 4 times before we count that as a successful scan.
The framerate on iOS is good, but the time to scan is not adequate.
We were previously using a fork of this library https://github.com/rodgomesc/vision-camera-code-scanner which also uses MLKit on iOS and it was significantly better and matched the Android implementation/performance.
Could iOS be changed to use MLKit instead of the native APIās?
Reproduceable Code
const codeScanner = useCodeScanner({
codeTypes: [
'code-128',
'code-39',
'code-93',
'codabar',
'ean-13',
'ean-8',
'itf',
'upc-e',
'qr',
'pdf-417',
'aztec',
'data-matrix',
],
onCodeScanned: (codes) => {
// ...
},
});
Relevant log output
NA
Camera Device
NA
Device
iPhone 13, iPad Mini
VisionCamera Version
3.5.1
Can you reproduce this issue in the VisionCamera Example app?
Yes, I can reproduce the same issue in the Example app here
Additional information
- I am using Expo
- I have enabled Frame Processors (react-native-worklets-core)
- I have read the Troubleshooting Guide
- I agree to follow this projectās Code of Conduct
- I searched for similar issues in this repository and found none.
About this issue
- Original URL
- State: closed
- Created 8 months ago
- Reactions: 3
- Comments: 24 (7 by maintainers)
@lc-mm to be honest - no.
As mentioned previously, the Code Scanner API is available under an
AVCaptureMetadataOutput
channel on iOS. This API is really straight forward, thatās all the code needed to scan QR codes on iOS: https://github.com/mrousavy/react-native-vision-camera/blob/8f986a45ea0aa3398e45a806a264fbcc03278971/package/ios/Core/CameraSession%2BConfiguration.swift#L130-L149If I wanted to use MLKit for this, I would have to:
video
+frameProcessor
+codeScanner
), I need to smartly decide which resolution I am going to pick and if there even needs to be a video channel.Hereās the downsides of the video channel approach compared to the
AVCaptureMetadataOutput
approach:format
, it will default to the highest resolution (4k frames) - which is going to be very slow in terms of MLKit Code Scanning performanceformat
, it will use the formatās width and height for the MLKit Code Scanner. This means, you canāt really do high quality video recordings/frame processing and codescanning in one - you have to either decide for code scanning (low resolution video) or video recordings (very slow code scanning) since again, itās all in one queue.AVCaptureMetadataOutput
channel.AVCaptureMetadataOutput
API is very battery/energy efficient. It might use pure GPU algorithms, benefit from raw YUV buffers, etc. In theory, it should be fast and lightweight.video
+frameProcessor
) from the QR code scanner (codeScanner
), meaning it can detect Barcodes without a hickup while youāre recording a video or processing Frames. It is running on a separate output channel. Thatās simply not possible with the video approachSo in short; Yes I know it absolutely sucks that the code scanner is only fast on QR codes for iOS, this is most definitely a bug since I donāt think Iām doing anything terribly wrong here. I can try to spend some more time to investigate this in a native app (no React Native) thanks to @ticketscloud for sponsoring me/this! ā¤ļø
@mrousavy when I started this thread I said that it was an apple issue and presented alternatives. Is this ever something you would change so that the performance of this feature is acceptable for barcodes on iOS? Currently it is not for anything other than QR codes.
I just tested @mgcrea/vision-camera-barcode-scanner and can confirm that the implementation there that uses the Vision APIs is much better at detecting barcodes than AVFoundation implementation
Is using either the Vision APIs or MLKit on iOS an option? In the current state it feels as if the iOS implementation is only good for QR codes
For future visitors of this issue: I had the same issue with very slow barcode scanning in iOS with vision-camera v3. I have upgraded yesterday to v4.0.1 and this solved the issue for me, scanning is now really fast. Thank you @mrousavy great job!
@ecaii We had this issue as well and weāve decided to use a frameprocessor plugin: https://github.com/mgcrea/vision-camera-barcode-scanner for the iOS part which is still faster & more reliable
I have the same experience. When I use vision-camera-v2 in our app to scan a barcode, it takes less than 1/100 second. But with vision-camera-v3 (3.6.4) on the same device I have to rotate the phone to fit the barcode vertically. (I guess thus it makes the barcode image larger.)
vision-camera-v2:
https://github.com/mrousavy/react-native-vision-camera/assets/46003022/683b1884-1d1a-42bf-8b10-d33b92632823
vision-camera-v3:
https://github.com/mrousavy/react-native-vision-camera/assets/46003022/592fd717-02d8-425e-9dba-b2755a19d8e8
@mrousavy
An update - Iām seeing barcodes that fail be recognized when scanned horizontally are consistently recognized when the barcode is turned vertically.
The phone orientation is vertical during all of this testing, for what itās worth.
Just wanted to chime in here and say that I too am seeing this issue. Non-QR barcodes take a considerable amount of time to be recognized (in the neighborhood of 8-10 seconds). Iāve tinkered with
enableBufferCompression
andformat
as @mrousavy recommended, but with no luck thus far.And to add something to the discussion, rather than just throwing in a useless ā+1ā:
Iāve tried iOS 16.4, iOS 17.0, and iOS 17.1 (released today).
MLKit on iOS is much better than iOS innner scan speed
I noticed this too, after some scans it looks like its needs more and more time to scan. On android it works fine. for ios fix i use temporary external barcode scanner ā@mgcrea/vision-camera-barcode-scannerā but only works with react-native-vision-camera 3.4.1