File: bitmap_generator.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (91 lines) | stat: -rw-r--r-- 3,310 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_QR_CODE_GENERATOR_BITMAP_GENERATOR_H_
#define COMPONENTS_QR_CODE_GENERATOR_BITMAP_GENERATOR_H_

#include "base/containers/span.h"
#include "base/types/expected.h"
#include "build/build_config.h"
#include "components/qr_code_generator/error.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"

namespace qr_code_generator {

// How to render QR code "pixels".
// This does not affect the main locators.
enum class ModuleStyle {
  kSquares,
  kCircles,
};

// Style for the corner locators.
enum class LocatorStyle {
  kSquare,
  kRounded,
};

// The center image to superimpose over the QR code.
enum class CenterImage {
  kNoCenterImage,
  kDino,
#if !BUILDFLAG(IS_IOS)
  kPasskey,
  kProductLogo,
#endif
};

// Whether `GenerateBitmap` will include the necessary quiet zone around the
// generated QR code.
enum class QuietZone {
  kIncluded,

  // WARNING: This option means that the caller of `GenerateBitmap` is
  // responsible for adding light-colored margins of `kQuietZoneSize` pixels
  // around the returned image.  See also `kQuietZoneSizePixels` below.
  //
  // TODO(https://crbug.com/325664342): Audit callers of `GenerateBitmap` and
  // see if they can/should use `kIncluded` instead (testing if the callers are
  // okay with differently-sized images and margins).  Note that the quiet zone
  // may help to detect the QR codes even for small codes.   Once all the users
  // of `kWillBeAddedByClient` are removed, the `QuietZone` enum can be removed
  // altogether.
  kWillBeAddedByClient,
};

// This gives the size of the required "quiet zone" that needs to be added to
// the image returned by `GenerateBitmap`.  The size is expressed in pixels of
// the returned `SkBitmap`.  A margin of light-colored pixels (that is this many
// pixels wide) needs to be added to the returned image on the left, right, top,
// and bottom.
//
// See also https://www.qrcode.com/en/howto/code.html which has diagrams and
// additional explanation about the "quiet zone".
extern const int kQuietZoneSizePixels;

// Generates a gfx::ImageSkia with a QR code that encodes the `data`.
base::expected<gfx::ImageSkia, Error> GenerateImage(
    base::span<const uint8_t> data,
    ModuleStyle module_style,
    LocatorStyle locator_style,
    CenterImage center_image,
    QuietZone quiet_zone);

// Generates an `SkBitmap` with a QR code that encodes the `data`.
//
// Use GenerateImage() if the QR code might be displayed on a high density
// (retina) display.
// TODO(lukasza, petewil): Consider letting the caller specify the exact light
// and dark colors.  (Currently white and black are always used.)
base::expected<SkBitmap, Error> GenerateBitmap(base::span<const uint8_t> data,
                                               ModuleStyle module_style,
                                               LocatorStyle locator_style,
                                               CenterImage center_image,
                                               QuietZone quiet_zone);

}  // namespace qr_code_generator

#endif  // COMPONENTS_QR_CODE_GENERATOR_BITMAP_GENERATOR_H_