-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #594 from ulan/ulan/qrcode
Add a QR code generator example
- Loading branch information
Showing
19 changed files
with
8,630 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Various IDEs and Editors | ||
.vscode/ | ||
.idea/ | ||
**/*~ | ||
|
||
# Mac OSX temporary files | ||
.DS_Store | ||
**/.DS_Store | ||
|
||
# dfx temporary files | ||
.dfx/ | ||
|
||
# generated files | ||
src/declarations/ | ||
|
||
# rust | ||
target/ | ||
|
||
# frontend code | ||
node_modules/ | ||
dist/ | ||
|
||
# environment variables | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[workspace] | ||
members = [ | ||
"src/qrcode_backend", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# QR code generator on the Internet Computer | ||
|
||
## Overview | ||
|
||
This example shows that an Internet Computer dapp can perform a long-running computation, like image processing, in a single message execution. | ||
This is possible due to a unique feature called Deterministic Time Slicing (DTS), which automatically divides long computations into smaller slices executed across multiple blocks. | ||
Developers can write long-running code as usual and don't require anything special to take advantage of DTS, as demonstrated in this example. | ||
|
||
You try the live version of the dapp running on the Internet Computer mainnet here: [https://khpe2-4qaaa-aaaao-a2fnq-cai.icp0.io/](https://khpe2-4qaaa-aaaao-a2fnq-cai.icp0.io/). | ||
|
||
## Prerequisites | ||
This example requires an installation of: | ||
|
||
- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx). | ||
- [x] Install `node.js` to build the web frontend. | ||
- [x] Clone this project to a local directory. | ||
|
||
## Running locally | ||
|
||
Start a local replica of the Internet Computer by running: | ||
|
||
``` | ||
dfx start --background | ||
``` | ||
|
||
You can omit the `--background` argument if you want to see log messages of the dapp. | ||
|
||
Now you can build and deploy the dapp with a single command | ||
``` | ||
dfx deploy | ||
``` | ||
|
||
If you see any error, it might be worthwhile to consult the [developer forum](https://forum.dfinity.org/). | ||
In case of successful deployment, you will see an output with local URLs: | ||
|
||
``` | ||
Deployed canisters. | ||
URLs: | ||
Frontend canister via browser | ||
qrcode_frontend: ... | ||
Backend canister via Candid interface: | ||
qrcode_backend: ... | ||
``` | ||
|
||
Navigate to the frontend URL in your browser and you'll be able to interact with the dapp. | ||
|
||
![Screenshot of the frontend UI](screenshot.png) | ||
|
||
|
||
## How it works | ||
|
||
The initial code of the dapp was autogenerated by `dfx` using the standard frontend/backend template. | ||
|
||
The frontend consists of an HTML page with a form where users can enter text for the QR code and choose various options. | ||
When the user clicks the "Generate!" button, a JavaScript handler initiates a call to the backend canister. | ||
The heavy-lifting of this call is managed by `candid`, `js-agent`, and `dfx`, which automatically generates a JavaScript object from the backend's Candid interface. | ||
That object contains `async` functions for each of the backend's endpoints, and the button handler uses them to make the calls. | ||
|
||
The backend, written in Rust, uses the `qrcode-generator`` and `image`` crates to create a QR code from user text. | ||
It also performs some image processing, to add the Internet Computer logo and a color gradient to the final result. | ||
Note the amount of computational work may be significant for large images. | ||
|
||
For educational purposes the backend offers two public endpoint for QR code generation: one for updates and another for queries. | ||
Currently, DTS is supported for updates, but not for queries. | ||
As a result, the update endpoint has larger instruction limit compared to the query endpoint and thus can handle larger images. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"canisters": { | ||
"qrcode_backend": { | ||
"candid": "src/qrcode_backend/qrcode_backend.did", | ||
"package": "qrcode_backend", | ||
"type": "rust" | ||
}, | ||
"qrcode_frontend": { | ||
"dependencies": [ | ||
"qrcode_backend" | ||
], | ||
"frontend": { | ||
"entrypoint": "src/qrcode_frontend/src/index.html" | ||
}, | ||
"source": [ | ||
"src/qrcode_frontend/assets", | ||
"dist/qrcode_frontend/" | ||
], | ||
"type": "assets" | ||
} | ||
}, | ||
"defaults": { | ||
"build": { | ||
"args": "", | ||
"packtool": "" | ||
} | ||
}, | ||
"output_env_file": ".env", | ||
"version": 1 | ||
} |
Oops, something went wrong.