-
Notifications
You must be signed in to change notification settings - Fork 236
#1784 add random fingerprint generator #1789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asolntsev
wants to merge
1
commit into
main
Choose a base branch
from
feature/1784-fingerprint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
172 changes: 172 additions & 0 deletions
172
src/main/java/net/datafaker/providers/base/Fingerprint.java
This file contains hidden or 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,172 @@ | ||
| package net.datafaker.providers.base; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
| import java.awt.image.BufferedImage; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.Base64; | ||
|
|
||
| /** | ||
| * Generates synthetic fingerprint images. | ||
| * <p> | ||
| * The generated images mimic the three standard fingerprint pattern types: | ||
| * whorl (spiral ridges), loop (ridges that loop around a core), and arch | ||
| * (ridges that arch over the centre). A random pattern is chosen unless one | ||
| * is supplied explicitly. | ||
| * | ||
| * @since 2.6.0 | ||
| */ | ||
| public class Fingerprint extends AbstractProvider<BaseProviders> { | ||
|
|
||
| private static final int DEFAULT_WIDTH = 200; | ||
| private static final int DEFAULT_HEIGHT = 250; | ||
| private static final double TWO_PI = 2 * Math.PI; | ||
|
|
||
| /** | ||
| * The three standard fingerprint ridge-flow patterns. | ||
| */ | ||
| public enum PatternType {WHORL, LOOP, ARCH} | ||
|
|
||
| protected Fingerprint(BaseProviders faker) { | ||
| super(faker); | ||
| } | ||
|
|
||
| /** | ||
| * Returns raw PNG bytes of a random-pattern fingerprint image. | ||
| */ | ||
| public byte[] png() { | ||
| return png(DEFAULT_WIDTH, DEFAULT_HEIGHT, randomType()); | ||
| } | ||
|
|
||
| /** | ||
| * Returns raw PNG bytes of a fingerprint image with the specified size and pattern. | ||
| */ | ||
| public byte[] png(int width, int height, PatternType pattern) { | ||
| return encodePng(render(width, height, pattern)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a base64 PNG data URL of a random-pattern fingerprint. | ||
| */ | ||
| public String base64() { | ||
| return base64(DEFAULT_WIDTH, DEFAULT_HEIGHT, randomType()); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a base64 PNG data URL of a random-pattern fingerprint image with the specified size and pattern. | ||
| */ | ||
| public String base64(int width, int height, PatternType pattern) { | ||
| byte[] png = png(width, height, pattern); | ||
| return "data:image/png;base64," + Base64.getEncoder().encodeToString(png); | ||
| } | ||
|
|
||
| private PatternType randomType() { | ||
| return faker.random().nextEnum(PatternType.class); | ||
| } | ||
|
|
||
| private byte[] encodePng(BufferedImage image) { | ||
| try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | ||
| ImageIO.write(image, "PNG", baos); | ||
| return baos.toByteArray(); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to encode fingerprint as PNG", e); | ||
| } | ||
| } | ||
|
|
||
| private BufferedImage render(int width, int height, PatternType patternType) { | ||
| BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); | ||
|
|
||
| // Core position – slightly randomised around the centre | ||
| double cx = width * (0.47 + faker.random().nextDouble(-0.04, 0.04)); | ||
| double cy = height * (0.47 + faker.random().nextDouble(-0.04, 0.04)); | ||
|
|
||
| // Ridge parameters | ||
| double ridgePeriod = 11.0 + faker.random().nextDouble(-2.0, 3.0); | ||
| double ridgeFraction = 0.42 + faker.random().nextDouble(-0.04, 0.06); | ||
|
|
||
| // Pattern-specific parameters | ||
| double spiralFactor = 0.5 + faker.random().nextDouble(0.0, 0.5); | ||
| double loopAmplitude = height * (0.18 + faker.random().nextDouble(0.0, 0.10)); | ||
| double loopSigma = Math.min(width, height) * (0.28 + faker.random().nextDouble(0.0, 0.12)); | ||
| double archAmplitude = height * (0.12 + faker.random().nextDouble(0.0, 0.08)); | ||
| double archSigma = width * (0.30 + faker.random().nextDouble(0.0, 0.15)); | ||
|
|
||
| // Organic displacement noise: three sine-wave components | ||
| int numNoise = 3; | ||
| double[] noiseAmp = {3.2, 1.6, 0.7}; | ||
| double[] noiseFreq = {0.014, 0.032, 0.068}; | ||
| double[] phasesX = new double[numNoise]; | ||
| double[] phasesY = new double[numNoise]; | ||
| for (int i = 0; i < numNoise; i++) { | ||
| phasesX[i] = faker.random().nextDouble(0.0, TWO_PI); | ||
| phasesY[i] = faker.random().nextDouble(0.0, TWO_PI); | ||
| } | ||
|
|
||
| // Oval mask (fingerprints are oval) | ||
| double maskA = width * 0.44; | ||
| double maskB = height * 0.46; | ||
| double fadeZone = 0.13; // fraction of mask radius used for edge fade | ||
|
|
||
| // Ink tones | ||
| int ridgeGray = faker.random().nextInt(25, 65); | ||
| int valleyGray = faker.random().nextInt(195, 235); | ||
|
|
||
| for (int y = 0; y < height; y++) { | ||
| for (int x = 0; x < width; x++) { | ||
|
|
||
| // Sine-wave displacement gives ridges an organic, non-straight look | ||
| double noiseX = 0, noiseY = 0; | ||
| for (int i = 0; i < numNoise; i++) { | ||
| double f = noiseFreq[i]; | ||
| noiseX += noiseAmp[i] * Math.sin(x * f + y * f * 0.6 + phasesX[i]); | ||
| noiseY += noiseAmp[i] * Math.sin(y * f + x * f * 0.6 + phasesY[i]); | ||
| } | ||
|
|
||
| double dx = x + noiseX - cx; | ||
| double dy = y + noiseY - cy; | ||
| double r = Math.sqrt(dx * dx + dy * dy); | ||
|
|
||
| // Ridge value – periodic over ridgePeriod | ||
| double ridgeVal = switch (patternType) { | ||
| case WHORL -> { | ||
| // Spiral: angle controls how much the ridges rotate | ||
| double theta = Math.atan2(dy, dx); // −PI … PI | ||
| yield r + spiralFactor * ridgePeriod * theta / Math.PI; | ||
| } | ||
| case LOOP -> { | ||
| // Pull the effective centre upward near the core to form a loop | ||
| double pull = loopAmplitude * Math.exp(-r * r / (2 * loopSigma * loopSigma)); | ||
| double loopDy = dy - pull; | ||
| yield Math.sqrt(dx * dx + loopDy * loopDy); | ||
| } | ||
| case ARCH -> { | ||
| // Ridges arch upward over the core | ||
| double arch = archAmplitude * Math.exp(-dx * dx / (2 * archSigma * archSigma)); | ||
| yield dy + arch; | ||
| } | ||
| }; | ||
|
|
||
| double mod = ((ridgeVal % ridgePeriod) + ridgePeriod) % ridgePeriod; | ||
| boolean isRidge = mod < ridgePeriod * ridgeFraction; | ||
|
|
||
| // Oval mask with smooth edge fade toward white | ||
| double mx = (x - width * 0.5) / maskA; | ||
| double my = (y - height * 0.5) / maskB; | ||
| double maskDist = mx * mx + my * my; | ||
|
|
||
| int pixel; | ||
| if (maskDist >= 1.0) { | ||
| pixel = 255; | ||
| } else { | ||
| double fade = Math.min(1.0, (1.0 - maskDist) / fadeZone); | ||
| int base = isRidge ? ridgeGray : valleyGray; | ||
| pixel = (int) Math.round(base * fade + 255.0 * (1.0 - fade)); | ||
| } | ||
|
|
||
| image.getRaster().setSample(x, y, 0, pixel); | ||
| } | ||
| } | ||
|
|
||
| return image; | ||
| } | ||
| } | ||
This file contains hidden or 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
99 changes: 99 additions & 0 deletions
99
src/test/java/net/datafaker/providers/base/FingerprintTest.java
This file contains hidden or 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,99 @@ | ||
| package net.datafaker.providers.base; | ||
|
|
||
| import net.datafaker.Faker; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.EnumSource; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
| import java.awt.image.BufferedImage; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.Base64; | ||
| import java.util.Random; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class FingerprintTest { | ||
|
|
||
| private final Faker faker = new Faker(); | ||
|
|
||
| @Test | ||
| void pngReturnsPngBytes() { | ||
| byte[] bytes = faker.fingerprint().png(); | ||
|
|
||
| assertThat(bytes).isNotEmpty(); | ||
| // PNG magic: 0x89 P N G | ||
| assertThat(bytes[0]).isEqualTo((byte) 0x89); | ||
| assertThat(bytes[1]).isEqualTo((byte) 'P'); | ||
| assertThat(bytes[2]).isEqualTo((byte) 'N'); | ||
| assertThat(bytes[3]).isEqualTo((byte) 'G'); | ||
| } | ||
|
|
||
| @Test | ||
| void pngHasExpectedDimensions() throws IOException { | ||
| byte[] bytes = faker.fingerprint().png(); | ||
|
|
||
| BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes)); | ||
| assertThat(image.getWidth()).isEqualTo(200); | ||
| assertThat(image.getHeight()).isEqualTo(250); | ||
| } | ||
|
asolntsev marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| void base64ReturnsDataUrl() { | ||
| String dataUrl = faker.fingerprint().base64(); | ||
|
|
||
| assertThat(dataUrl).startsWith("data:image/png;base64,"); | ||
| String encoded = dataUrl.substring("data:image/png;base64,".length()); | ||
| assertThat(encoded).isNotBlank().isBase64(); | ||
| } | ||
|
|
||
| @Test | ||
| void base64DecodesBackToPng() throws IOException { | ||
| String dataUrl = faker.fingerprint().base64(); | ||
|
|
||
| String encoded = dataUrl.substring("data:image/png;base64,".length()); | ||
| byte[] bytes = Base64.getDecoder().decode(encoded); | ||
| BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes)); | ||
| assertThat(image).isNotNull(); | ||
| assertThat(image.getWidth()).isEqualTo(200); | ||
| assertThat(image.getHeight()).isEqualTo(250); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource(Fingerprint.PatternType.class) | ||
| void eachPatternTypeProducesValidPng(Fingerprint.PatternType pattern) throws IOException { | ||
| byte[] bytes = faker.fingerprint().png(400, 500, pattern); | ||
|
|
||
| assertThat(bytes).isNotEmpty(); | ||
| assertThat(bytes[0]).isEqualTo((byte) 0x89); | ||
| assertThat(bytes[1]).isEqualTo((byte) 'P'); | ||
|
|
||
| BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes)); | ||
| assertThat(image.getWidth()).isEqualTo(400); | ||
| assertThat(image.getHeight()).isEqualTo(500); | ||
|
asolntsev marked this conversation as resolved.
|
||
|
|
||
| File folder = new File("target/surefire-reports"); | ||
| folder.mkdirs(); | ||
| ImageIO.write(image, "png", new File(folder, "fingerprint-" + pattern + ".png")); | ||
|
asolntsev marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| void twoPngCallsProduceDifferentImages() { | ||
| byte[] first = faker.fingerprint().png(); | ||
| byte[] second = faker.fingerprint().png(); | ||
|
|
||
| // Extremely unlikely to be identical given randomised parameters | ||
| assertThat(first).isNotEqualTo(second); | ||
|
asolntsev marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| void twoPngCallsWithSameSeeProduceSameImages() { | ||
| long seed = System.nanoTime(); | ||
| byte[] first = new Faker(new Random(seed)).fingerprint().png(); | ||
| byte[] second = new Faker(new Random(seed)).fingerprint().png(); | ||
|
|
||
| assertThat(first).isEqualTo(second); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.