Skip to content

Commit

Permalink
remove dependency to com.google.zxing:javase
Browse files Browse the repository at this point in the history
  • Loading branch information
kryptonbutterfly committed Jan 24, 2024
1 parent 821df4c commit f8c5cfb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@
<artifactId>core</artifactId>
<version>3.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
71 changes: 71 additions & 0 deletions src/kryptonbutterfly/totp/misc/ImageLuminanceSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package kryptonbutterfly.totp.misc;

import static kryptonbutterfly.math.utils.range.Range.*;

import java.awt.image.BufferedImage;

import com.google.zxing.LuminanceSource;

import kryptonbutterfly.math.utils.limit.LimitInt;

public final class ImageLuminanceSource extends LuminanceSource
{
private final BufferedImage image;

public ImageLuminanceSource(BufferedImage image)
{
this(image, image.getWidth(), image.getHeight());
}

public ImageLuminanceSource(BufferedImage image, int width, int height)
{
super(width, height);
if (image.getType() == BufferedImage.TYPE_BYTE_GRAY)
this.image = image;
else
{
this.image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

for (final int y : range(height))
for (final int x : range(width))
this.image.setRGB(x, y, toLuminance(image.getRGB(x, y)));
}
}

private static final int toLuminance(int argb)
{
final int alpha = (argb >>> 24) & 0xFF;
if (alpha == 0)
return 0xFF;

final int red = (argb >> 16) & 0xFF;
final int green = (argb >> 8) & 0xFF;
final int blue = argb & 0xFF;
return (306 * red + 601 * green + 117 * blue + 0x200) >> 10;
}

@Override
public byte[] getRow(int y, byte[] row)
{
if (LimitInt.inRange(0, y, getHeight() - 1))
throw new IllegalArgumentException("Requested row is outside the image: " + y);

final int width = getWidth();

if (row == null || row.length < width)
row = new byte[width];
image.getRaster().getDataElements(0, y, width, 1, row);

return row;
}

@Override
public byte[] getMatrix()
{
final int width = getWidth();
final int height = getHeight();
final var matrix = new byte[width * height];
image.getRaster().getDataElements(0, 0, width, height, matrix);
return matrix;
}
}
4 changes: 2 additions & 2 deletions src/kryptonbutterfly/totp/ui/qrimport/BL.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

import kryptonbutterfly.monads.opt.Opt;
import kryptonbutterfly.totp.TinyTotp;
import kryptonbutterfly.totp.misc.ImageLuminanceSource;
import kryptonbutterfly.util.swing.Logic;
import kryptonbutterfly.util.swing.events.GuiCloseEvent;
import kryptonbutterfly.util.swing.events.GuiCloseEvent.Result;
Expand Down Expand Up @@ -136,7 +136,7 @@ private String decode(BufferedImage image)
ChecksumException,
FormatException
{
final var bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
final var bitmap = new BinaryBitmap(new HybridBinarizer(new ImageLuminanceSource(image)));
final var res = new QRCodeReader().decode(bitmap, HINTS);
return res.getText();
}
Expand Down
1 change: 0 additions & 1 deletion src/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
requires lombok;
requires webcam.capture;
requires com.google.zxing;
requires com.google.zxing.javase;
}

0 comments on commit f8c5cfb

Please sign in to comment.