A react native application to detect color waves using mobile camera
source link: https://reactnativeexample.com/a-react-native-application-to-detect-color-waves-using-mobile-camera/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
A react native application to detect color waves using mobile camera
Colorwaves
An app to detect colorwaves (swatches/palettes) in the real world โ powered by VisionCamera and Reanimated.
I wrote this app in less than a day, a speed simply not matched by native app development. Because itโs written in React Native (TypeScript/JavaScript), it runs both on iOS and Android, but performance critical parts (e.g. the image processing algorithm or the animations) are backed by native Objective-C/Swift/Java code.
See this Tweet for more information.
Try it!
Download the repository and run the following commands to try Colorwaver for yourself:
yarn try-ios
Android
yarn try-android
Project structure
This is a bare React Native project, created with create-react-native-app.
- ๐
src
: Contains the actual TypeScript + React (-Native) front-end for the Colorwaver App.-
๐
src/getColorPalette.ts
: Exposes the native iOS/Android frame processor plugin as a JS function with TypeScript types. This function has to be called from a frame processor. ('worklet'
) -
๐
src/Router.tsx
: The main component that gets registered byindex.js
. This acts as a main navigator, navigating either to the Splash Page (Permissions) or the App Page (Main App) depending on whether the user has granted permissions or not. -
๐
src/Splash.tsx
: The first Splash page to ask the user for Camera permission. -
๐
src/App.tsx
: Contains the actual source code for the Appโs front-end.- VisionCamera is used to get a Camera device and display a Camera component. Also, a frame processor (a function that gets called for every frame the camera โseesโ) gets attached, which calls the native iOS/Android frame processor plugin.
- Reanimated is used to smoothly animate between color changes.
Because VisionCamera also uses the Worklet API, the entire process between receiving a camera frame and actually displaying the paletteโs colors does not use the React-JS Thread at all.
The frame processing runs on a separate Thread from VisionCamera, which then dispatches the animations on the Reanimated UI Thread.
This is why the App runs as smooth as a native iOS or Android app. -
๐
src/useAnimatedColor.ts
: A helper function to animate color changes withSharedValue
s.
-
- ๐
ios
: Contains the basic skeleton for a React Native iOS app, plus the nativegetColorPalette(...)
Frame Processor Plugin.-
๐
ios/PaletteFrameProcessorPlugin.m
: Declares the Swift frame processor plugin โgetColorPalette(...)
โ. -
๐
ios/PaletteFrameProcessorPlugin.swift
: Contains the actual Swift code for the native iOS frame processor plugin โgetColorPalette(...)
โ.This uses the CoreImage API to convert the
CMSampleBuffer
to aUIImage
, and then uses theUIImageColors
library to build the color palette.VisionCameraโs frame processor API is used to expose this function as a frame processor plugin.
-
๐
ios/Colorwaver-Bridging-Header.h
: A Bridging Header to import Objective-C headers into Swift. -
๐
ios/Podfile
: Adds theUIImageColors
library. -
๐
ios/UIColor+hexString.swift
: An extension forUIColor
to convertUIColor
instances to strings. This is required because React Native handles colors as strings.
-
- ๐
android
: Contains the basic skeleton for a React Native Android app, plus the nativegetColorPalette(...)
Frame Processor Plugin.-
๐
android/app/build.gradle
: The gradle build file for the Android project. The following third-party dependencies are installed:androidx.camera
:camera-core
androidx.palette
:palette
-
๐
android/app/src/main/java/com/colorwaver/utils
: Contains two files copied from android/camera-samples to convertImageProxy
instances toBitmap
s. (YuvToRgbConverter.kt
) -
๐
android/app/src/main/java/com/colorwaver
: Contains the actual Java source code of the Project. -
๐
android/app/src/main/java/com/colorwaver/MainApplication.java
: Sets up react-native-reanimated. -
๐
android/app/src/main/java/com/colorwaver/MainActivity.java
: Installs thePaletteFrameProcessorPlugin
frame processor plugin inside of theonCreate
method. -
๐
android/app/src/main/java/com/colorwaver/PaletteFrameProcessorPlugin.java
: Contains the actual Java code for the native Android frame processor plugin โgetColorPalette(...)
โ.This uses the
YuvToRgbConverter
to convert theImageProxy
to aBitmap
, and then passes that to the Palette API from AndroidX to build the color palette.VisionCameraโs frame processor API is used to expose this function as a frame processor plugin.
-
- ๐
babel.config.js
: Adds the native frame processor plugingetColorPalette
(called__getColorPalette
) to Reanimatedโsglobal
list.
Credits
- @Simek for the original idea
- react-native-reanimated for allowing JS code to be dispatched to another Runtime (Worklets)
- react-native-vision-camera for allowing you to run JS code on a realtime feed of Camera Frames (Frame Processors)
- The Palette API from AndroidX for getting the Color Palette on Android
UIImageColors
for getting the Color Palette on iOS- You guys, for downloading, giving 5 stars, and pushing Colorwaver up in the Charts.
GitHub
The Next-Gen Crypto Wallet built using React Native
Football App UI Built With React Native
You might also like...
Subscribe to React Native Example for Android and iOS
Get the latest posts delivered right to your inbox
Recommend
-
11
React Native Healthcare Scheduling Mobile Application July 24...
-
14
Audiobook App Open Source Audiobook mobile application accomplished with React Native and React ecosystem, just a single code base for both android and ios. Installation git clone [email protected]:minhtc/audiobo...
-
22
Using React Native Keychain in Your Mobile Application React Native Keychain is a library that helps keep your mobile application secure with the added co...
-
12
Nyxo App Nyxo is a mobile application for improving your sleep. Its built with React Native, AWS Amplify, styled-components, and Redux. Nyxo provides the following features: Sleep tracking, with support for multiple diffe...
-
6
Aline ! Aline is an Android and iOS mobile application that helps you manage your returnable products: Find stores that offer returnable products Find restaurants that offer returnable meal boxes Scan a...
-
7
Shop App This project is a simplified implementation of a shopping system, the project aims to provide the basic features that are expected to be found in a mobile e-commerce applicaion where users can purchase and sell products...
-
4
Netflix A netflix clone mobile application using React Native Aug 23, 2021...
-
4
GAtec React Native boilerplate This project is a React Nativeboilerplate that can be used to kickstart a mobile application. The boilerplate provides an optimized architecture for building solid cross-platform mob...
-
5
Social Marketplace is a Mobile Application built with React Native. Description Social Marketplace to sell what you donโt need anymore. the app supports registering and authenticate using email & password in addition to...
-
10
Apps SportsHub - A React Native mobile application that aims to connect athletes all across New Zealand Jun 15, 2022 3 min r...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK