### Get Segment Data Bits - Rust Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/struct.QrSegment Provides read-only access to the raw bit buffer representing the encoded data of the QrSegment. ```rust pub fn data(&self) -> &Vec ``` -------------------------------- ### Initialize QR Code Image Dimensions Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs_search= This function calculates the necessary dimensions and margins for rendering a QR code image. It determines the `point_size` based on the desired output `size` and the QR code's data length, ensuring the `point_size` is at least 1. It returns an error if the requested image size is too small to render the QR code. ```rust fn to_image_inner(qr: QrCode, size: usize) -> Result, QRCodeError> { if size >= 2usize.pow((size_of::() * 4) as u32) { return Err(QRCodeError::ImageSizeTooLarge); } let margin_size = 1; let s = qr.size(); let data_length = s as usize; let data_length_with_margin = data_length + 2 * margin_size; let point_size = size / data_length_with_margin; if point_size == 0 { return Err(QRCodeError::ImageSizeTooSmall); } let margin = (size - (point_size * data_length)) / 2; // ... rest of the function Ok(Vec::new()) // Placeholder for actual image data generation } ``` -------------------------------- ### Get Segment Character Count - Rust Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/struct.QrSegment Returns the number of characters represented by the data in the QrSegment. This count is specific to the encoding mode of the segment. ```rust pub fn num_chars(&self) -> usize ``` -------------------------------- ### PNG Image Generation from String Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/fn.to_png_to_writer_from_str Encodes a given string into a PNG image and writes it to a specified writer. Supports customizable error correction levels and image size. ```APIDOC ## POST /qrcode/generate/png ### Description Encodes text into a PNG image format and writes the output to a provided writer. This function allows for customization of error correction level and the size of the generated QR code. ### Method POST ### Endpoint `/qrcode/generate/png` ### Parameters #### Query Parameters - **text** (string) - Required - The text content to be encoded into a QR code. - **ecc** (string) - Required - The error correction level. Accepted values: `Low`, `Medium`, `Quartile`, `High`. - **size** (integer) - Required - The desired size of the QR code image in pixels. #### Request Body This endpoint does not accept a request body. All parameters are passed via query parameters. ### Request Example ```bash curl -X POST "http://your-api.com/qrcode/generate/png?text=Hello%20World&ecc=Medium&size=256" ``` ### Response #### Success Response (200) - **image/png** (bytes) - The generated PNG image data of the QR code. #### Response Example (Binary PNG data would be returned here, not representable as JSON) ``` -------------------------------- ### QR Code Generation Functions Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/all_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E This section details the various functions available for generating QR codes in different formats and from different input types. ```APIDOC ## QR Code Generation Functions This section details the various functions available for generating QR codes in different formats and from different input types. ### Functions - **`to_image`** - Description: Generates a QR code as an image. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_image_buffer`** - Description: Generates a QR code as an image buffer. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_image_buffer_from_segments`** - Description: Generates a QR code as an image buffer from QR segments. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_image_buffer_from_str`** - Description: Generates a QR code as an image buffer from a string. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_image_from_segments`** - Description: Generates a QR code as an image from QR segments. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_image_from_str`** - Description: Generates a QR code as an image from a string. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_matrix`** - Description: Generates a QR code as a matrix. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_matrix_from_segments`** - Description: Generates a QR code as a matrix from QR segments. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_matrix_from_str`** - Description: Generates a QR code as a matrix from a string. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_file`** - Description: Encodes QR code data to a PNG file. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_file_from_segments`** - Description: Encodes QR code data from segments to a PNG file. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_file_from_str`** - Description: Encodes QR code data from a string to a PNG file. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_vec`** - Description: Encodes QR code data to a PNG byte vector. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_vec_from_segments`** - Description: Encodes QR code data from segments to a PNG byte vector. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_vec_from_str`** - Description: Encodes QR code data from a string to a PNG byte vector. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_writer`** - Description: Encodes QR code data to a PNG writer. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_writer_from_segments`** - Description: Encodes QR code data from segments to a PNG writer. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_png_to_writer_from_str`** - Description: Encodes QR code data from a string to a PNG writer. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_file`** - Description: Encodes QR code data to an SVG file. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_file_from_segments`** - Description: Encodes QR code data from segments to an SVG file. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_file_from_str`** - Description: Encodes QR code data from a string to an SVG file. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_string`** - Description: Encodes QR code data to an SVG string. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_string_from_segments`** - Description: Encodes QR code data from segments to an SVG string. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_string_from_str`** - Description: Encodes QR code data from a string to an SVG string. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_writer`** - Description: Encodes QR code data to an SVG writer. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_writer_from_segments`** - Description: Encodes QR code data from segments to an SVG writer. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A - **`to_svg_to_writer_from_str`** - Description: Encodes QR code data from a string to an SVG writer. - Method: (Assumed to be a function call, not an HTTP endpoint) - Endpoint: N/A ``` -------------------------------- ### Get Segment Mode - Rust Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/struct.QrSegment Retrieves the mode indicator of a QrSegment, specifying how the data within the segment is encoded (e.g., numeric, alphanumeric, byte). ```rust pub fn mode(&self) -> QrSegmentMode ``` -------------------------------- ### Get TypeId of a Type in Rust Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/struct.QrSegment_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Implements the `Any` trait, providing a `type_id` method. This allows for runtime type identification, which can be useful in generic programming contexts. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Compare QrSegments for Equality in Rust Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/struct.QrSegment_search=u32+-%3E+bool Implements the `PartialEq` trait for `QrSegment`, enabling comparison of two segment instances for equality based on their content and mode. ```rust fn eq(&self, other: &QrSegment) -> bool ``` -------------------------------- ### QR Code Generation Functions Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/all_search= This section details the various functions available for generating QR codes. These functions allow for the creation of QR codes in different formats like images (PNG) and SVGs, and can accept input as strings or segments. Error correction levels can also be specified. ```APIDOC ## QR Code Generation Functions ### Description This group of functions provides the core capabilities for generating QR codes. You can generate QR codes as matrices, PNG images, or SVG representations. Input can be provided as raw strings or pre-defined QR segments, and you can specify the desired error correction level. ### Functions * **`to_image`**: Generates a QR code as an image. * **`to_image_buffer`**: Generates a QR code as an image buffer. * **`to_image_buffer_from_segments`**: Generates a QR code from segments as an image buffer. * **`to_image_buffer_from_str`**: Generates a QR code from a string as an image buffer. * **`to_image_from_segments`**: Generates a QR code from segments as an image. * **`to_image_from_str`**: Generates a QR code from a string as an image. * **`to_matrix`**: Generates a QR code as a matrix. * **`to_matrix_from_segments`**: Generates a QR code from segments as a matrix. * **`to_matrix_from_str`**: Generates a QR code from a string as a matrix. * **`to_png_to_file`**: Encodes and writes a QR code to a PNG file. * **`to_png_to_file_from_segments`**: Encodes and writes a QR code from segments to a PNG file. * **`to_png_to_file_from_str`**: Encodes and writes a QR code from a string to a PNG file. * **`to_png_to_vec`**: Encodes and returns a QR code as a PNG byte vector. * **`to_png_to_vec_from_segments`**: Encodes and returns a QR code from segments as a PNG byte vector. * **`to_png_to_vec_from_str`**: Encodes and returns a QR code from a string as a PNG byte vector. * **`to_png_to_writer`**: Encodes and writes a QR code to a PNG writer. * **`to_png_to_writer_from_segments`**: Encodes and writes a QR code from segments to a PNG writer. * **`to_png_to_writer_from_str`**: Encodes and writes a QR code from a string to a PNG writer. * **`to_svg_to_file`**: Encodes and writes a QR code to an SVG file. * **`to_svg_to_file_from_segments`**: Encodes and writes a QR code from segments to an SVG file. * **`to_svg_to_file_from_str`**: Encodes and writes a QR code from a string to an SVG file. * **`to_svg_to_string`**: Encodes and returns a QR code as an SVG string. * **`to_svg_to_string_from_segments`**: Encodes and returns a QR code from segments as an SVG string. * **`to_svg_to_string_from_str`**: Encodes and returns a QR code from a string as an SVG string. * **`to_svg_to_writer`**: Encodes and writes a QR code to an SVG writer. * **`to_svg_to_writer_from_segments`**: Encodes and writes a QR code from segments to an SVG writer. * **`to_svg_to_writer_from_str`**: Encodes and writes a QR code from a string to an SVG writer. ### Enums * **`QRCodeError`**: Enum representing possible errors during QR code generation. * **`QrCodeEcc`**: Enum representing the error correction levels for QR codes (e.g., Low, Medium, Quartile, High). ### Structs * **`QrSegment`**: Represents a segment of data within a QR code. ### Example Usage (Conceptual) ```rust // Example of generating a QR code to a PNG file use qrcode_generator::to_png_to_file; let data = "https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/"; let file_path = "qrcode.png"; match to_png_to_file(data, file_path) { Ok(_) => println!("QR code saved to {}", file_path), Err(e) => eprintln!("Error generating QR code: {:?}", e), } // Example of generating a QR code to an SVG string use qrcode_generator::to_svg_to_string; let svg_string = to_svg_to_string(data, qrcode_generator::QrCodeEcc::High).unwrap(); println!("SVG QR Code:\n{}", svg_string); ``` ``` -------------------------------- ### Generate SVG to String Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs This function converts a QR code into an SVG string. It leverages the `to_svg_to_vec_inner` function to get the SVG as bytes and then converts the byte vector into a UTF-8 string. This is an unsafe operation as it assumes the generated SVG is valid UTF-8. ```rust fn to_svg_to_string_inner>(qr: QrCode, size: usize, description: Option) -> Result { let svg = to_svg_to_vec_inner(qr, size, description)?; Ok(unsafe { String::from_utf8_unchecked(svg) }) } ``` -------------------------------- ### Implement From for QRCodeError Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/enum.QRCodeError_search=u32+-%3E+bool Provides a conversion implementation from a standard Error type into QRCodeError, allowing seamless error handling when I/O errors occur. ```rust impl From for QRCodeError { fn from(error: Error) -> Self { // ... implementation details ... } } ``` -------------------------------- ### QR Code Generation Functions Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/all_search=u32+-%3E+bool This section details the various functions available for generating QR codes in different formats like images, matrices, and SVG. ```APIDOC ## QR Code Generation Functions This section details the various functions available for generating QR codes in different formats like images, matrices, and SVG. ### Functions - **`to_image`** - Description: Generates a QR code as an image. - Method: (Assumed based on naming convention, likely involves a return type of an image format) - Endpoint: N/A (This is a library function, not a web API endpoint) - **`to_image_buffer`** - Description: Generates a QR code as an image buffer. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_image_buffer_from_segments`** - Description: Generates a QR code as an image buffer from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_image_buffer_from_str`** - Description: Generates a QR code as an image buffer from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_image_from_segments`** - Description: Generates a QR code as an image from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_image_from_str`** - Description: Generates a QR code as an image from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_matrix`** - Description: Generates a QR code as a matrix. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_matrix_from_segments`** - Description: Generates a QR code as a matrix from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_matrix_from_str`** - Description: Generates a QR code as a matrix from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_file`** - Description: Encodes a QR code to PNG format and saves it to a file. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_file_from_segments`** - Description: Encodes a QR code to PNG format and saves it to a file from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_file_from_str`** - Description: Encodes a QR code to PNG format and saves it to a file from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_vec`** - Description: Encodes a QR code to PNG format and returns it as a byte vector. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_vec_from_segments`** - Description: Encodes a QR code to PNG format and returns it as a byte vector from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_vec_from_str`** - Description: Encodes a QR code to PNG format and returns it as a byte vector from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_writer`** - Description: Encodes a QR code to PNG format and writes it to a writer. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_writer_from_segments`** - Description: Encodes a QR code to PNG format and writes it to a writer from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_png_to_writer_from_str`** - Description: Encodes a QR code to PNG format and writes it to a writer from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_file`** - Description: Encodes a QR code to SVG format and saves it to a file. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_file_from_segments`** - Description: Encodes a QR code to SVG format and saves it to a file from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_file_from_str`** - Description: Encodes a QR code to SVG format and saves it to a file from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_string`** - Description: Encodes a QR code to SVG format and returns it as a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_string_from_segments`** - Description: Encodes a QR code to SVG format and returns it as a string from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_string_from_str`** - Description: Encodes a QR code to SVG format and returns it as a string from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_writer`** - Description: Encodes a QR code to SVG format and writes it to a writer. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_writer_from_segments`** - Description: Encodes a QR code to SVG format and writes it to a writer from provided segments. - Method: (Assumed based on naming convention) - Endpoint: N/A - **`to_svg_to_writer_from_str`** - Description: Encodes a QR code to SVG format and writes it to a writer from a string. - Method: (Assumed based on naming convention) - Endpoint: N/A ``` -------------------------------- ### PNG Generation to File Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs_search=std%3A%3Avec Functions to encode data, text, or segments into a PNG image and save it to a specified file path. ```APIDOC ## POST /generate/png/file ### Description Encodes data, text, or segments into a PNG image and saves it to a file. ### Method POST ### Endpoint `/generate/png/file` ### Parameters #### Request Body - **data** (bytes | string | QrSegment[]) - Required - The data to encode into the QR code. - **ecc** (QrCodeEcc) - Required - The error correction level. - **size** (usize) - Required - The desired size of the PNG image. - **path** (string) - Required - The file path to save the PNG image. ### Request Example ```json { "data": "https://example.com", "ecc": "Quartile", "size": 200, "path": "/path/to/save/qrcode.png" } ``` ### Response #### Success Response (200) - **status** (string) - "success" #### Error Response (400/500) - **error** (string) - Description of the error. ``` -------------------------------- ### QR Code Generation to PNG File Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/fn.to_png_to_file_from_str_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E This function encodes provided text into a QR code and saves it as a PNG image to a specified file path. It allows customization of error correction level and image size. ```APIDOC ## `to_png_to_file_from_str` ### Description Encodes text into a QR code and saves it as a PNG image to a specified file path. Allows customization of error correction level and image size. ### Method Not Applicable (This is a library function, not an HTTP endpoint) ### Endpoint Not Applicable ### Parameters #### Path Parameters - **path** (`P: AsRef`) - Required - The file path where the PNG image will be saved. #### Query Parameters None #### Request Body None (This is a function call, not an HTTP request body) ### Request Example ```rust use qrcode_generator::to_png_to_file_from_str; use qrcode_generator::QrCodeEcc; use std::path::Path; let text = "https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/"; let ecc = QrCodeEcc::Low; let size = 256; let path = Path::new("qrcode.png"); match to_png_to_file_from_str(text, ecc, size, path) { Ok(_) => println!("QR code saved successfully to qrcode.png"), Err(e) => eprintln!("Error generating QR code: {:?}", e), } ``` ### Response #### Success Response - **Result<(), QRCodeError>** - Returns `Ok(())` on successful generation and saving, or an error if the process fails. #### Response Example ```json // On success, the function returns Ok(()) // On failure, it returns Err(QRCodeError::SomeErrorType) ``` ``` -------------------------------- ### PNG Generation to File Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs_search= Functions to encode data into a PNG image and save it to a specified file path. Supports encoding from raw data, strings, or QR code segments. ```APIDOC ## PNG Generation to File ### Description Encodes data into a PNG image and saves it to a specified file path. Supports encoding from raw data, strings, or QR code segments. ### Methods - `to_png_to_file(data, ecc, size, path)` - `to_png_to_file_from_str(text, ecc, size, path)` - `to_png_to_file_from_segments(segments, ecc, size, path)` ### Parameters #### Common Parameters - **ecc** (QrCodeEcc) - Error correction level. - **size** (usize) - The desired size of the QR code image. - **path** (P: AsRef) - The file path to save the PNG image. #### Data Input - **data** (D: AsRef<[u8]>) - Raw byte data to encode. - **text** (S: AsRef) - String data to encode. - **segments** (segments: &[QrSegment]) - Pre-defined QR code segments to encode. ### Response - **Result<(), QRCodeError>** - Returns Ok(()) on success, or a QRCodeError on failure. ``` -------------------------------- ### QR Code Generation Functions Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/index_search=std%3A%3Avec This section details the various functions available in the qrcode-generator crate for encoding data into QR codes. It covers generating QR codes as matrices, PNG images (in memory or file), and SVG images (in memory or file). ```APIDOC ## QR Code Generation Functions This crate provides functions to generate QR Code matrices and images in RAW, PNG and SVG formats. ### Encode any data to a QR Code matrix (`Vec>`) ```rust use qrcode_generator::QrCodeEcc; let result: Vec> = qrcode_generator::to_matrix("Hello world!", QrCodeEcc::Low).unwrap(); println!("{:?}", result); ``` ### Encode any data to a PNG image stored in a `Vec` ```rust use qrcode_generator::QrCodeEcc; let result: Vec = qrcode_generator::to_png_to_vec("Hello world!", QrCodeEcc::Low, 1024).unwrap(); println!("{:?}", result); ``` ### Encode any data to a PNG image stored in a file ```rust use qrcode_generator::QrCodeEcc; qrcode_generator::to_png_to_file("Hello world!", QrCodeEcc::Low, 1024, "tests/data/file_output.png").unwrap(); ``` ### Encode any data to a SVG image stored in a `String` ```rust use qrcode_generator::QrCodeEcc; let result: String = qrcode_generator::to_svg_to_string("Hello world!", QrCodeEcc::Low, 1024, None::<&str>).unwrap(); println!("{:?}", result); ``` ### Encode any data to a SVG image stored in a file ```rust use qrcode_generator::QrCodeEcc; qrcode_generator::to_svg_to_file("Hello world!", QrCodeEcc::Low, 1024, None::<&str>, "tests/data/file_output.png").unwrap(); ``` ### Low-level Usage - Raw Image Data The `to_image` and `to_image_buffer` functions can be used if you want to modify your image. ### Low-level Usage - Segments Every `to_*` function has a corresponding `_from_segments` function. You can concatenate segments by using different encoding methods, such as **numeric**, **alphanumeric**, or **binary** to reduce the size (level) of your QR code matrix/image. ```rust use qrcode_generator::{QrCodeEcc, QrSegment}; let first = "1234567"; let second = "ABCDEFG"; let segments = [QrSegment::make_numeric(&first), QrSegment::make_alphanumeric(&second)]; let result: Vec> = qrcode_generator::to_matrix_from_segments(&segments, QrCodeEcc::Low).unwrap(); println!("{:?}", result); ``` More segments optimization approaches: [magiclen/qrcode-segments-optimizer](https://github.com/magiclen/qrcode-segments-optimizer) ### Available Functions: * `to_image`: Encode data to raw image in memory. * `to_image_buffer`: Encode data to a image buffer. * `to_image_buffer_from_segments`: Encode segments to a image buffer. * `to_image_buffer_from_str`: Encode text to a image buffer. * `to_image_from_segments`: Encode segments to raw image in memory. * `to_image_from_str`: Encode text to raw image in memory. * `to_matrix`: Encode data to a QR code matrix. * `to_matrix_from_segments`: Encode segments to a QR code matrix. * `to_matrix_from_str`: Encode text to a QR code matrix. * `to_png_to_file`: Encode data to a PNG image via a file path. * `to_png_to_file_from_segments`: Encode text to a PNG image via a file path. * `to_png_to_file_from_str`: Encode text to a PNG image via a file path. * `to_png_to_vec`: Encode data to a PNG image in memory. * `to_png_to_vec_from_segments`: Encode segments to a PNG image in memory. * `to_png_to_vec_from_str`: Encode text to a PNG image in memory. * `to_png_to_writer`: Encode data to a PNG image via a writer. * `to_png_to_writer_from_segments`: Encode segments to a PNG image via a writer. * `to_png_to_writer_from_str`: Encode text to a PNG image via a writer. * `to_svg_to_file`: Encode data to a SVG image via a file path. * `to_svg_to_file_from_segments`: Encode segments to a SVG image via a file path. * `to_svg_to_file_from_str`: Encode text to a SVG image via a file path. * `to_svg_to_string`: Encode data to a SVG image in memory. * `to_svg_to_string_from_segments`: Encode segments to a SVG image in memory. * `to_svg_to_string_from_str`: Encode text to a SVG image in memory. * `to_svg_to_writer`: Encode data to a SVG image via a writer. * `to_svg_to_writer_from_segments`: Encode segments to a SVG image via a writer. * `to_svg_to_writer_from_str`: Encode text to a SVG image via a writer. ``` -------------------------------- ### Generate QR Code to PNG File (Rust) Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs Encodes data into a PNG QR code and saves it directly to a file path. This function simplifies the process of creating QR code image files. Requires the `image` feature to be enabled. Supports encoding raw data, strings, or segments. ```rust #[cfg(feature = "image")] /// Encode data to a PNG image via a file path. #[inline] pub fn to_png_to_file, P: AsRef>( data: D, ecc: QrCodeEcc, size: usize, path: P, ) -> Result<(), QRCodeError> { to_png_to_file_inner(generate_qrcode(data, ecc)?, size, path) } #[cfg(feature = "image")] /// Encode text to a PNG image via a file path. #[inline] pub fn to_png_to_file_from_str, P: AsRef>( text: S, ecc: QrCodeEcc, size: usize, path: P, ) -> Result<(), QRCodeError> { to_png_to_file_inner(generate_qrcode_from_str(text, ecc)?, size, path) } #[cfg(feature = "image")] /// Encode text to a PNG image via a file path. #[inline] pub fn to_png_to_file_from_segments>( segments: &[QrSegment], ecc: QrCodeEcc, size: usize, path: P, ) -> Result<(), QRCodeError> { to_png_to_file_inner(generate_qrcode_from_segments(segments, ecc)?, size, path) } ``` -------------------------------- ### Encode Data to PNG File (Rust) Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs_search=u32+-%3E+bool Encodes input data into a PNG image and saves it directly to a specified file path. Requires the 'image' feature to be enabled. Image size is configurable. ```rust use qrcode_generator::QrCodeEcc; # #[cfg(feature = "image")] { qrcode_generator::to_png_to_file("Hello world!", QrCodeEcc::Low, 1024, "tests/data/file_output.png").unwrap(); # } ``` -------------------------------- ### Implement From for QRCodeError Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/enum.QRCodeError_search=u32+-%3E+bool Provides a conversion implementation from an ImageError type into QRCodeError. This feature is only available when the 'image' crate feature is enabled. ```rust impl From for QRCodeError { fn from(error: ImageError) -> Self { // ... implementation details ... } } ``` -------------------------------- ### Encode Data to PNG File (Rust) Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs Generates a PNG image from input data and saves it directly to a specified file path. This function simplifies the process of creating QR code image files. Requires the 'image' feature to be enabled. ```rust use qrcode_generator::QrCodeEcc; // #[cfg(feature = "image")] { qrcode_generator::to_png_to_file("Hello world!", QrCodeEcc::Low, 1024, "tests/data/file_output.png").unwrap(); // } ``` -------------------------------- ### Generate PNG QR Code to File (Rust) Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs_search=std%3A%3Avec Encodes QR code data into a PNG file at the specified path. This function handles file creation and writing, ensuring the file is removed if an error occurs during PNG encoding. Requires the 'image' feature. ```Rust #[cfg(feature = "image")] #[inline] fn to_png_to_file_inner>( qr: QrCode, size: usize, path: P, ) -> Result<(), QRCodeError> { let path = path.as_ref(); let file = File::create(path)?; to_png_inner(qr, size, file).map_err(|err| { if fs::remove_file(path).is_err() { // do nothing } err }) } ``` -------------------------------- ### Create Byte Segment - Rust Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/struct.QrSegment Creates a QrSegment for raw binary data. This is useful for encoding any byte slice, including UTF-8 encoded strings, into a QR code. ```rust pub fn make_bytes(data: &[u8]) -> QrSegment ``` -------------------------------- ### to_png_to_file_from_str Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/fn.to_png_to_file_from_str_search=std%3A%3Avec Encodes the provided text into a PNG image and saves it to a specified file path. Supports custom error correction levels and image sizes. ```APIDOC ## POST /qrcode-generator/to_png_to_file_from_str ### Description Encodes text into a PNG image and saves it to a file path. ### Method POST ### Endpoint `/qrcode-generator/to_png_to_file_from_str` ### Parameters #### Request Body - **text** (string) - Required - The text content to encode into the QR code. - **ecc** (string) - Required - The error correction level (e.g., "low", "medium", "high", "highest"). - **size** (integer) - Required - The desired size of the QR code image in pixels. - **path** (string) - Required - The file path where the PNG image will be saved. ### Request Example ```json { "text": "https://example.com", "ecc": "medium", "size": 256, "path": "/path/to/save/qrcode.png" } ``` ### Response #### Success Response (200) - **status** (string) - Indicates success. #### Response Example ```json { "status": "success" } ``` #### Error Response (400/500) - **error** (string) - Description of the error. #### Error Response Example ```json { "error": "Invalid ECC level provided." } ``` ``` -------------------------------- ### Implement Display for QRCodeError Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/enum.QRCodeError_search=u32+-%3E+bool Implements the Display trait for the QRCodeError enum, enabling user-friendly string representations of errors. ```rust impl Display for QRCodeError { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { // ... implementation details ... } } ``` -------------------------------- ### to_svg_to_file Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/fn.to_svg_to_file_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Encodes provided data into an SVG image and saves it to a specified file path. Supports error correction levels, custom sizes, and optional descriptions. ```APIDOC ## to_svg_to_file ### Description Encodes data into an SVG image and saves it to a specified file path. This function allows for customization of error correction level, image size, and an optional description for the SVG. ### Method This is a function within the `qrcode_generator` crate, not a traditional HTTP API endpoint. ### Endpoint N/A (Local function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature ```rust pub fn to_svg_to_file, DESC: AsRef, P: AsRef>( data: D, ecc: QrCodeEcc, size: usize, description: Option, path: P, ) -> Result<(), QRCodeError> ``` ### Parameters Explained - **data** (AsRef<[u8]>): The data to be encoded into the QR code. Can be a byte slice or anything that can be referenced as a byte slice. - **ecc** (QrCodeEcc): The error correction level for the QR code (e.g., Low, Medium, High, Quartile). - **size** (usize): The desired size of the generated QR code image in pixels. - **description** (Option>): An optional string to be used as a description within the SVG file. - **path** (AsRef): The file path where the generated SVG image will be saved. ### Return Value - **Result<(), QRCodeError>**: - `Ok(())` on successful generation and saving of the SVG file. - `Err(QRCodeError)` if any error occurs during the process (e.g., file writing error, data encoding error). ``` -------------------------------- ### Rust TryFrom and TryInto for Fallible Conversions Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/struct.QrSegment_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E The `TryFrom` and `TryInto` traits enable fallible type conversions, where a conversion might fail. They return a `Result` type, indicating success with the converted value or failure with an associated error type. ```rust /// The type returned in the event of a conversion error. type Error = Infallible; /// Performs the conversion. fn try_from(value: U) -> Result>::Error>; /// The type returned in the event of a conversion error. type Error = >::Error; /// Performs the conversion. fn try_into(self) -> Result>::Error>; ``` -------------------------------- ### PNG Generation to Vec Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs_search=std%3A%3Avec Functions to encode data, text, or segments into a PNG image and return it as a byte vector in memory. ```APIDOC ## POST /generate/png/vec ### Description Encodes data, text, or segments into a PNG image and returns the image data as a byte vector. ### Method POST ### Endpoint `/generate/png/vec` ### Parameters #### Request Body - **data** (bytes | string | QrSegment[]) - Required - The data to encode into the QR code. - **ecc** (QrCodeEcc) - Required - The error correction level. - **size** (usize) - Required - The desired size of the PNG image. ### Request Example ```json { "data": "https://example.com", "ecc": "Low", "size": 128 } ``` ### Response #### Success Response (200) - **png_data** (bytes) - The PNG image data as a byte vector. #### Error Response (400/500) - **error** (string) - Description of the error. ``` -------------------------------- ### Generate Image from QR Code in Rust Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E This function generates an image from a QR code. It takes a QrCode and the desired size as input. It calculates margins and point sizes to determine the dimensions of each module in the QR code. It also includes a check to prevent the image size from exceeding a maximum value, returning an error if the size is too large. ```rust 324fn to_image_inner(qr: QrCode, size: usize) -> Result, QRCodeError> { 325 if size >= 2usize.pow((size_of::() * 4) as u32) { 326 return Err(QRCodeError::ImageSizeTooLarge); 327 } 328 329 let margin_size = 1; 330 331 let s = qr.size(); 332 333 let data_length = s as usize; 334 335 let data_length_with_margin = data_length + 2 * margin_size; 336 337 let point_size = size / data_length_with_margin; 338 339 if point_size == 0 { 340 return Err(QRCodeError::ImageSizeTooSmall); 341 } 342 343 let margin = (size - (point_size * data_length)) / 2; 344 ``` -------------------------------- ### Encode Data to PNG File using Rust Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/fn.to_png_to_file Encodes input data into a PNG image file using the specified error correction level and size. This function takes data, ECC level, image size, and a file path as input, returning a Result indicating success or a QRCodeError. It requires the `qrcode-generator` crate. ```Rust pub fn to_png_to_file, P: AsRef>( data: D, ecc: QrCodeEcc, size: usize, path: P, ) -> Result<(), QRCodeError> ``` -------------------------------- ### Encode Data to PNG (File) Source: https://docs.rs/qrcode-generator/5.0.0/qrcode_generator/index_search= Encodes data into a PNG image and saves it directly to a specified file path. Supports custom image size and error correction. ```APIDOC ## POST /qrcode-generator/png/file ### Description Encodes the provided data into a PNG image and saves it to the specified file path. ### Method POST ### Endpoint /qrcode-generator/png/file ### Parameters #### Query Parameters - **ecc** (QrCodeEcc) - Required - The error correction level for the QR code. - **size** (u32) - Optional - The desired size of the PNG image in pixels. - **file_path** (string) - Required - The path where the PNG image will be saved. #### Request Body - **data** (string) - Required - The data to encode. ### Request Example ```json { "data": "Hello world!" } ``` ### Response #### Success Response (200) - **message** (string) - A confirmation message indicating the file was saved successfully. #### Response Example ```json { "message": "QR code image saved to tests/data/file_output.png" } ``` ``` -------------------------------- ### Generate QR Code to PNG Writer (Rust) Source: https://docs.rs/qrcode-generator/5.0.0/src/qrcode_generator/lib.rs Encodes data into a PNG QR code and writes it to a provided `Write` trait implementor. This is useful for generating PNG QR codes to various output streams. Requires the `image` feature to be enabled. Supports encoding raw data, strings, or segments. ```rust #[cfg(feature = "image")] /// Encode data to a PNG image via a writer. #[inline] pub fn to_png_to_writer, W: Write>( data: D, ecc: QrCodeEcc, size: usize, writer: &mut W, ) -> Result<(), QRCodeError> { to_png_inner(generate_qrcode(data, ecc)?, size, writer) } #[cfg(feature = "image")] /// Encode text to a PNG image via a writer. #[inline] pub fn to_png_to_writer_from_str, W: Write>( text: S, ecc: QrCodeEcc, size: usize, writer: &mut W, ) -> Result<(), QRCodeError> { to_png_inner(generate_qrcode_from_str(text, ecc)?, size, writer) } #[cfg(feature = "image")] /// Encode segments to a PNG image via a writer. #[inline] pub fn to_png_to_writer_from_segments( segments: &[QrSegment], ecc: QrCodeEcc, size: usize, writer: &mut W, ) ```