This Demo demonstrates scanning barcodes from the camera and displaying the scanning results to the corresponding screen positions.
The corresponding blueprint is here: '/Game/ZXingTest/UI/WBP_TestReader.WBP_TestReader', you can search this blueprint in the Content Browser:
Open it in Graph mode:
In this blueprint, I will explain each event from top to bottom
In order to open Camera you should first request the permission of the camera. The first Custom Event "CameraScan" will request Android Camera Permissions and call another Custom Event "OnPermissionGranted" when the camera permission is granted. You can double click the "Create Event" node to jump to the define location of the Event or right click on "CameraScan" to find the references of that Event.
2. The second Custom Event "OnPermissionGranted" will find available camera and call "OpenUrl" to open the the camera it found and bind the Custom Event "OnCameraOpened". The Media player will call this Event when the camera is opened.
3. The third event is a built-in event of Unreal Engine, which is called when the entire UI is built. Here I bind ScanImageButton and StartScanButton, and call the corresponding event when the corresponding button is pressed. You can double-click the CreateEvent node to jump to the relevant definition:
4. The fourth event, LoadTestImage, first judges whether camera scanning is in progress. If not, a picture to be tested will be displayed on the screen, and at the same time, the new binding of the LoadTestImage button will be changed into ScanTestImage.
5. The fifth event, ScanTestImage, first judges whether the camera scan is in progress. If not, it will call the Decode function provided by this plug-in to decode. In order to display the recognized four vertices in the corresponding positions of the unreal SlateUI system, it is necessary to convert the The corresponding function for converting coordinates to SlateUI's coordinate system is "DisplayAreaLocalToViewport"
6. The sixth event is a built-in event of Unreal Engine, which is called when the UI is destroyed, and MediaPlayer will be closed here.
7. The seventh event, CleanScanResult, will remove all the red lines and green text drawn on the screen by calling ScanTestImage, and at the same time change the event triggered by the button that called this event to LoadTestImage
8. The eighth event, OnCameraOpened, will be called when the camera is turned on. It will display a small scan area on the screen, and only the pictures in this area will be decoded. For how to open the camera, please refer to the documentation of Unreal Engine.
The function of UpdateScanner in this event is to update the small scanning area so that it adapts to different screen sizes.
At the same time, a delay of 0.6 seconds is added in order not to directly scan the pictures in the last cache
9. When the ninth event CloseCamera is called, it will close the camera scan and adjust the related UI at the same time
10. The tenth event, Event Tick, is a built-in event of Unreal Engine, which will be called every time the UI is updated. In order to obtain the texture of the camera, here I call CreateRenderTarget2D and draw with the material of the camera to get an image captured by the camera, and call Decode to decode, a parameter InRect of Decode refers to the sub-area to be decoded, at this time it is the small bright area to be scanned
11. The 11th event fires when the TestWriterButton is pressed, It will switch to the Encode UI.
The corresponding blueprint is here: '/Game/ZXingTest/UI/WBP_TestWriter.WBP_TestWriter'
There are two public functions in this plugin, which are used for decode and encode.
The official AndroidCameraPlayer plugin has limited functions. If you need auto focus, set the preview size and other functions, you need to modify it yourself.
In this Demo I modified its code to support auto focus. Sorry i can't share the code of the engine here, please check the engine code yourself:
[UE Root]\Engine\Plugins\Media\AndroidCamera.
This plugin is based on ZXing Library. It used to support iPhone, but now I don't have Mac and iPhone to test this plugin. If you have a compilation environment and are familiar with programming, you can try to modify the ZXing modoule under:
{UE Install Path}\Engine\Plugins\Marketplace\ZXingForUE4\Source\ZXing