Created
October 20, 2014 07:31
-
-
Save jbollacke/a535a982ebd9f10da9bb to your computer and use it in GitHub Desktop.
Tries to workaround ZBars unreliable charset detection for qr codes.
This file contains 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
public class ZBarCharsetDetectionFixer { | |
private static final CharsetDecoder latin1CharsetDecoder = Charset.forName("ISO-8859-1").newDecoder(); | |
private static final CharsetEncoder sjisCharsetEncoder = Charset.forName("SJIS").newEncoder(); | |
/** | |
* Tries to workaround ZBars unreliable charset detection for qr codes. | |
* | |
* What it does: | |
* Encodes String to Shift-JS, decodes the resulting bytes to ISO-8859-1. | |
* | |
* @param input The input string from ZBar. | |
* @return Correct encoded string. | |
*/ | |
public String fixWeirdness(String input) { | |
try { | |
return latin1CharsetDecoder.decode(sjisCharsetEncoder.encode(CharBuffer.wrap(input))).toString(); | |
} catch (CharacterCodingException e) { | |
// this should be okay b/c it probably was not detected as shift-jis | |
} | |
return input; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment