Im trying to use ZXing library to develop a Java project for decoding a QR code. However, some of the image containing QR code can not be decoded by running my project, but these are working fine with Online ZXing decoder. I am just curious does the ZXing released version is the same as they are using for Online decoder? or they have tweaked the online version. I'm pulling my hair because of this confusion.
public class Validator implements IValidator {
private static Logger logger = Logger.getLogger(Validator.class);
private BufferedImage currentImage;
private String resultText;
private float moduleSize;
private ResultPoint[] patternCenters;
private int blockSizePower;
public Validator(BufferedImage imageFile) {
this.currentImage = imageFile;
setLuminanceThreshold(3); //default value used by validator
}
public Validator(File imageFile) {
// take input image file and store in a BufferedImage variable
try {
currentImage = ImageIO.read(imageFile);
} catch (IOException e) {
logger.error("Image cannot be opened. There is no such image file. ", e);
}
}
/**
* <p>Validating the QR code</p>
*
* #return true if the QR code can be decoded
*/
#Override
public boolean validateQRCode() {
return validateQRCode(null);
}
public boolean validateQRCode(Hashtable outValues) {
return validateQRCode(outValues, true);
}
// if localLuminanceCheck == true then call HybridBinarizer, otherwise call GlobalHistogramBinarizer
public boolean validateQRCode(Hashtable outValues, boolean localLuminanceCheck)
{
return validateQRCode(outValues, true, false);
}
public boolean validateQRCode(Hashtable outValues, boolean localLuminanceCheck, boolean scale) {
if (scale)
{
try {
this.currentImage = Thumbnails.of(currentImage).size(275, 275).asBufferedImage();
} catch (IOException e) {
logger.error("Image cannot be scaled. ", e);
}
}
// finding luminance of the image
LuminanceSource lumSource = new BufferedImageLuminanceSource(currentImage);
Binarizer qrHB;
if (!localLuminanceCheck) {
qrHB = new GlobalHistogramBinarizer(lumSource);
} else {
// creating binary bitmap from Black-White image
qrHB = new HybridBinarizer(lumSource);
((HybridBinarizer) qrHB).setBLOCK_SIZE_POWER(blockSizePower);
}
BinaryBitmap bitmap = new BinaryBitmap(qrHB);
try {
currentImage = MatrixToImageWriter.toBufferedImage(bitmap.getBlackMatrix());
} catch (NotFoundException e) {
logger.error("cannot find any bit matrix.", e);
}
Hashtable<DecodeHintType, Object> hint = new Hashtable<DecodeHintType, Object>();
hint.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE);
QRCodeReader QRreader = new QRCodeReader();
try {
// decodes the QR code
Result result = QRreader.decode(bitmap, hint);
resultText = result.getText();
return true;
} catch (NotFoundException e) {
logger.info("cannot detect any QR code (no enough finder patterns).");
return false;
} catch (ChecksumException e) {
logger.info("cannot recover the QR code. Too much data errors.");
return false;
} catch (FormatException e) {
logger.info("QR code cannot be decoded.");
return false;
} catch (FinderPatternNotFoundException e) {
// if no Finder Pattern has been found, it may be the color of
// QR is inverted. So we invert the QR and try one more time
Binarizer invertHB;
if (!localLuminanceCheck) {
invertHB = new GlobalHistogramBinarizer(lumSource);
} else {
invertHB = new HybridBinarizer(lumSource);
((HybridBinarizer) invertHB).setBLOCK_SIZE_POWER(blockSizePower);
}
// get the inverted Black-White matrix
BitMatrix invertBlackMatrix = null;
try {
invertBlackMatrix = invertHB.getBlackMatrix();
} catch (NotFoundException e1) {
logger.error(e1);
}
int invertWidth = currentImage.getWidth();
int invertHeight = currentImage.getHeight();
// flip each bit in the inverted BitMatrix
for (int x = 0; x < invertWidth; x++) {
for (int y = 0; y < invertHeight; y++) {
invertBlackMatrix.flip(x, y);
}
}
currentImage = MatrixToImageWriter.toBufferedImage(invertBlackMatrix);
// get luminance source from inverted image
lumSource = new BufferedImageLuminanceSource(currentImage);
Binarizer afterInvertHB;
if (!localLuminanceCheck) {
afterInvertHB = new GlobalHistogramBinarizer(lumSource);
} else {
// creating binary bitmap from Black-White image
afterInvertHB = new HybridBinarizer(lumSource);
((HybridBinarizer) afterInvertHB).setBLOCK_SIZE_POWER(blockSizePower);
}
BinaryBitmap invertBitMap = new BinaryBitmap(afterInvertHB);
// decoding inverted QR
QRCodeReader invertQRreader = new QRCodeReader();
try {
Result invertResult = invertQRreader.decode(invertBitMap, hint);
resultText = invertResult.getText();
System.out.println("Out put data is: " + resultText);
return true;
} catch (NotFoundException e1) {
logger.info("cannot detect any QR code (no enough finder patterns).");
return false;
} catch (ChecksumException e1) {
logger.info("cannot recover the QR code. Too much data errors.");
return false;
} catch (FormatException e1) {
logger.info("QR code cannot be decoded.");
return false;
} catch (FinderPatternNotFoundException e1) {
logger.info("Cannot confirm where all three Finder Patterns are! ");
return false;
} catch (Exception e1) {
logger.error(e1);
return false;
}
} catch (Exception e) {
logger.error(e);
return false;
}
}
}
It's not different, it's probably that you are not using TRY_HARDER mode, or are not trying both binarizers. The online version will do those things.
Related
The problem I am having is acquiring all reachable clients on a network.The below method returns some clients when called. In most cases other android clients.However for the PC it fails when firewall is on.Is there a more effective way to get all clients in Java/android purely or will I need to use android NDK?Any help from experts in this domain will be appreciated.Thanks in advance.
/***
* ping_JavaStyle(final int j)
* uses multi threads to enhance performance
* while pinging from 0>j<=255
* #param j
*/
private void ping_JavaStyle(final int j)
{
new Thread(new Runnable() { // new thread for parallel execution
public void run() {
try {
String testIp = prefix + String.valueOf(j);
InetAddress address = InetAddress.getByName(testIp);
String output = address.toString().substring(1);
if (address.isReachable(3000)) {
System.out.println(output + " is on the network");
ipList.add(testIp);
} else {
if (retest(testIp, 139)) {
ipList.add(testIp);
} else {
System.out.println("Not Reachable: " + output);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
After Researching some more, got this working.With help of this repo:https://github.com/stealthcopter/AndroidNetworkTools
Below code solves the problem:
** RunnableTask.Java
* Created by Kirk on 10/29/2017.
*/
public class RunnableTask implements Callable<Boolean> {
private String testIp = "";
private Boolean is_Reachable = false;
public RunnableTask(String testIp) {
this.testIp = testIp;
}
#Override
public Boolean call() throws Exception {
try {
PingResult pingResult = Ping.onAddress(this.testIp).setTimes(1).setTimeOutMillis(1500).doPing();
if (pingResult.isReachable) {
is_Reachable = true;
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
return is_Reachable;
}
}
And use in the caller method:
private static final int NTHREDS = 255;
//.......
ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
List<Future<Boolean>> thread_Values_list = new ArrayList<>();
for (int i = 1; i <= 255; i++) {
final int j = i;
try {
try {
String testIp = prefix + String.valueOf(j);
RunnableTask worker = new RunnableTask(testIp);
Future<Boolean> submit = executor.submit(worker);
thread_Values_list.add(submit);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
}
}
for (Future<Boolean> finishedThread : thread_Values_list) {
String reachable_Ip = "";
try {
if (finishedThread.get()) {
reachable_Ip = prefix + String.valueOf(finishThread_counter);
ipList.add(reachable_Ip);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
finishThread_counter++;
}
executor.shutdown();
}
I am trying to make an application to scan documents from a scanner and i have found an application based on the mmscomputing free library that i found in github https://github.com/ashishkataria/browserWebScanning
It shows a panel from where you can choose a scanner from a list of available ones and scan the document.
public void getScan()
{
try
{
scanner.acquire();
}
catch (ScannerIOException e1)
{
IJ.showMessage("Access denied! \nTwain dialog maybe already opened!");
e1.printStackTrace();
}
}
public Image getImage()
{
Image image = imp.getImage();
return image;
}
public void update(ScannerIOMetadata.Type type, ScannerIOMetadata metadata) {
if (type.equals(ScannerIOMetadata.ACQUIRED))
{
if(imp!=null)
{
jContentPane.remove(ipanel);
jContentPane.remove(cpanel);
jContentPane.remove(crpdpanel);
}
imp = new ImagePlus("Scan", metadata.getImage());
im = imp.getImage();
imagePanel = new ImagePanel(im);
imagePanel.updateUI();
imagePanel.repaint();
imagePanel.revalidate();
ClipMover mover = new ClipMover(imagePanel);
imagePanel.addMouseListener(mover);
imagePanel.addMouseMotionListener(mover);
ipanel = imagePanel.getPanel();
ipanel.setBorder(new LineBorder(Color.blue,1));
ipanel.setBorder(BorderFactory.createTitledBorder("Scanned Image"));
ipanel.setBounds(0, 30,600, 600);
ipanel.repaint();
ipanel.revalidate();
ipanel.updateUI();
jContentPane.add(ipanel);
jContentPane.getRootPane().revalidate();
jContentPane.updateUI();
cpanel = imagePanel.getUIPanel();
cpanel.setBounds(700, 30,300, 150);
cpanel.repaint();
cpanel.setBorder(new LineBorder(Color.blue,1));
cpanel.setBorder(BorderFactory.createTitledBorder("Cropping Image"));
cpanel.setBackground(Color.white);
jContentPane.add(cpanel);
jContentPane.repaint();
jContentPane.revalidate();
metadata.setImage(null);
try {
new uk.co.mmscomputing.concurrent.Semaphore(0, true).tryAcquire(2000, null);
} catch (InterruptedException e) {
IJ.error(e.getMessage());
}
}
else if (type.equals(ScannerIOMetadata.NEGOTIATE)) {
ScannerDevice device = metadata.getDevice();
try {
device.setResolution(100);
} catch (ScannerIOException e) {
IJ.error(e.getMessage());
}
try{
device.setShowUserInterface(true);
device.setResolution(100); }catch(Exception e){
e.printStackTrace(); }
}
else if (type.equals(ScannerIOMetadata.STATECHANGE)) {
System.out.println("Scanner State "+metadata.getStateStr());
System.out.println("Scanner State "+metadata.getState());
if ((metadata.getLastState() == 3) && (metadata.getState() == 4)){}
} else if (type.equals(ScannerIOMetadata.EXCEPTION)) {
IJ.error(metadata.getException().toString());
}
}
I am trying to make it handle multiple documents and save in a pdf file, this library handles only 1 document right now.
i want to know how can i save the images in a buffer or something else and rescan until the user is done .
and is there a function that can save those images in a pdf file ?
When a user clicks on a link, I want to change the page and then load the relevant info.
However, the HTMLElement is always null.
The following is the code:
WebViewPanel.java
//to be used in JFX Thread
public void loadURLFX(final String url) {
if (Misc.stringHasContent(url)) {
String tmp = toURL(url);
if (tmp == null) {
tmp = toURL("http://" + url);
}
boolean invalidURL = false;
InputStream stream = null;
try {
URL urlObj = new URL(tmp);
stream = urlObj.openStream();
} catch (Exception e) {
invalidURL = true;
} finally {
try {
stream.close();
} catch (Exception ex) {
}
stream = null;
}
if (!invalidURL) {
engine.load(tmp);
} else {
//https://www.google.com.sg/search?q=searchTerm
engine.load("https://www.google.com.sg/search?q=" + url);
}
}
}
App.java
public void load(){
if(Platform.isFxApplicationThread()){
webViewPanel.loadURLFX(App.class.getResource("/hello.html").toExternalForm());
HTMLElement element = (HTMLElement) WebMisc.getHTMLElement(webViewPanel.getWebEngine(), "menu");
}
}
element is always null. What should I do to fix it?
I'm busy creating a modification/plugin to an existing application, my modification includes a Rhino JavaScript scripting engine, the only problem I have is that the ScriptEngine and ScriptEngineManager aren't serializable, this wouldn't be a problem if I could save all the data. How could I do this?
var vertices = RenderUtils.createSquareVertices(64);
var indices = RenderUtils.createSquareIndices();
var mesh = RenderUtils.newStaticMesh(vertices, indices, true);
var textureThingy = RenderUtils.newTexture("NULL.png", Sys.getAssetDir());
var materialThingy = RenderUtils.newMaterial(textureThingy, Math.newVector3f(1, 1, 1), 1, 1);
var renderMesh = RenderUtils.newMeshRenderer(mesh, materialThingy);
var rotationAngle = 0.0;
function update() {
if(Mouse.isButtonDown(0)) rotationAngle=rotationAngle + 1.0;
if(Mouse.isButtonDown(1)) rotationAngle=rotationAngle - 1.0;
}
function render() {
renderMesh.getTransform().getPos().setVector(Sys.getWidth()/2, Sys.getHeight()/2, 0);
renderMesh.getTransform().setRotation(Math.newVector3f(0, 0, 1), rotationAngle);
renderMesh.render();
}
The java side:
public void start() {
try {
this.Script.eval(this.ApplicationCode);
} catch(ScriptException e) {
e.printStackTrace();
}
}
public void update() {
if(this.hasUpdateFunc) {
try {
if(this.Script != null) ((Invocable)this.Script).invokeFunction("update");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
public void render() {
if(this.hasRenderFunc) {
try {
if(this.Script != null) ((Invocable)this.Script).invokeFunction("render");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
public class Midlet extends MIDlet implements CommandListener{
Player p;
public void startApp() {
Display.getDisplay(this).setCurrent(new SongsList(this));
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void commandAction(Command cmnd, Displayable dsplbl) {
if (cmnd.getLabel().equals("Exit"))
{
destroyApp(true);
}
else
{
try {
//InputStream is = getClass().getResourceAsStream("/res/getlucky.mpeg");
//p = Manager.createPlayer(is, "audio/mpeg");
p = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
p.realize();
p.start();
} catch (IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}
}
}
}
this is the songslist class :
public class SongsList extends List{
public SongsList(Midlet midlet)
{
super("Songs", List.IMPLICIT);
append("get lucky", null);
addCommand(new Command("Exit", Command.EXIT, 0));
addCommand(new Command("Select", Command.OK, 0));
setCommandListener(midlet);
}
}
tried use via file stored in project (its under src/res):
inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");
as well as from HTTP:
//audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
Nothing works, what am I doing wrong?
EDIT:
I've tried to delete my application and just copy paste it to a new project and it worked for some reason.. now I encounter new problems:
1) I try to play a song - this is the link http://puu.sh/6n9jC.mp3
its not playing so I guess there's a limited file size for what can be played can someone tell me what is this limit ?
2) Im trying to record the audio with RecordPlayer but its always null
public AudioAnalyzer()
{
try {
thread = new Thread(this);
recordFinished = false;
//inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
//audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");
audioPlayer = Manager.createPlayer("http://puu.sh/35YTG.mp3");
//audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
audioPlayer.realize();
System.out.println(System.getProperty("supports.audio.capture"));
recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
recordOutput = new ByteArrayOutputStream();
recordControl.setRecordStream(recordOutput);
recordControl.startRecord();
audioPlayer.start();
//thread.start();
} catch (MediaException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
I even tried to print if the system is supporting audio capture and the result were true but I get NullPointException at this line :
recordOutput = new ByteArrayOutputStream();
although I tried to get the recordcontrol from the player it is still null :
recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
I think I read that it'll always give NullPointerException unless you run it on a real device and not an emulator is that true ? can someone verify it ? and if so what can I do if I don't own a device currently any other way to use recordcontrol feature in emulator (assuming recordcontrol isn't working on emulators).
File size is 8MB (maybe play on your phone), try to this code
public void initMedia(final String aFileUrl) {
if (m_player == null) {
try {
m_player = Manager.createPlayer(aFileUrl);
m_player.addPlayerListener(this);
m_player.realize();
m_player.prefetch();
m_volumeControl = (VolumeControl) m_player.getControl("VolumeControl");
} catch (IOException ex) {
} catch (Exception ex) {
} catch (OutOfMemoryError e) {
}
}
}
In your code, i guess you miss "m_player.prefetch()", try this. And print your Exception message...
This code in general for file, resourcce, http...
public void initMedia(final String aProtocol, final String aMediaSource) {
if (m_player == null) {
try {
if (aMediaSource.indexOf("file://") == 0) {
InputStream iRecordStream = Connector.openInputStream(aMediaSource);
m_player = Manager.createPlayer(iRecordStream, "audio/amr");
} else {
m_player = Manager.createPlayer(aProtocol);
}
m_player.addPlayerListener(this);
m_player.realize();
boolean isPrefetch = true;
try {
m_player.prefetch();
} catch (Exception ex) {
isPrefetch = false;
}
// trick to pass prefetch error
if (!isPrefetch) {
if (m_player != null) {
m_player.close();
m_player = null;
}
if (aMediaSource.indexOf("file://") == 0) {
InputStream iRecordStream = Connector.openInputStream(aMediaSource);
m_player = Manager.createPlayer(iRecordStream, "audio/amr");
} else {
m_player = Manager.createPlayer(aProtocol);
}
m_player.addPlayerListener(this);
m_player.realize();
m_player.prefetch();
}
m_volumeControl = (VolumeControl) m_player.getControl("VolumeControl");
} catch (IOException ex) {
} catch (Exception ex) {
} catch (OutOfMemoryError e) {
}
}
}
In general when it comes to J2ME development, you should always test your app on multiple real devices.
Emulators can't be trusted.
Also, J2ME is very fragmented, and various devices have various bugs and behaves differently with the same code. This will affect any app on many areas. One area being audio playback.
For example, some devices requires that you use the realize() and prefetch() methods, while other devices will crash if you use prefetch(). The only possible solution (if you wish to support as many devices as possible) is to use multiple try/catch blocks.
See this link for a detailed explanation and other tips'n'tricks on audio playback with MIDP2.0
http://indiegamemusic.com/help.php?id=1