Skip to content

Instantly share code, notes, and snippets.

@jbollacke
Created October 20, 2014 07:31
Show Gist options
  • Save jbollacke/a535a982ebd9f10da9bb to your computer and use it in GitHub Desktop.
Save jbollacke/a535a982ebd9f10da9bb to your computer and use it in GitHub Desktop.
Tries to workaround ZBars unreliable charset detection for qr codes.
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