Skip to content

Commit

Permalink
修复android4.4生成二维码失败的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
devilsen committed Aug 2, 2019
1 parent be91e94 commit 7658e38
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
19 changes: 17 additions & 2 deletions czxing/src/main/cpp/JNIUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,30 @@ bool AnalysisBrightness(JNIEnv *env, const jbyte *bytes, int width, int height)
* string转wstring
*/
std::wstring StringToWString(const std::string &src) {
unsigned len = src.size() * 2; // 预留字节数
setlocale(LC_CTYPE, ""); // 必须调用此函数
unsigned long len = src.size() * 2; // 预留字节数
setlocale(LC_CTYPE, ""); // 必须调用此函数,但是会造成污染
auto *p = new wchar_t[len]; // 申请一段内存存放转换后的字符串
mbstowcs(p, src.c_str(), len); // 转换
std::wstring desc(p);
delete[] p; // 释放申请的内存
return desc;
}

std::wstring ANSIToUnicode(const std::string &str) {
std::wstring ret;
std::mbstate_t state = {};
const char *src = str.data();
size_t len = std::mbsrtowcs(nullptr, &src, 0, &state);
if (static_cast<size_t>(-1) != len) {
std::unique_ptr<wchar_t[]> buff(new wchar_t[len + 1]);
len = std::mbsrtowcs(buff.get(), &src, len, &state);
if (static_cast<size_t>(-1) != len) {
ret.assign(buff.get(), len);
}
}
return ret;
}

void ThrowJavaException(JNIEnv *env, const char *message) {
static jclass jcls = env->FindClass("java/lang/RuntimeException");
env->ThrowNew(jcls, message);
Expand Down
1 change: 1 addition & 0 deletions czxing/src/main/cpp/JNIUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ std::shared_ptr<ZXing::BinaryBitmap> BinaryBitmapFromJavaBitmap(JNIEnv* env, job
std::shared_ptr<ZXing::BinaryBitmap> BinaryBitmapFromBytes(JNIEnv* env, void *grayscale, int cropLeft, int cropTop, int cropWidth, int cropHeight);
bool AnalysisBrightness(JNIEnv* env,const jbyte *bytes, int width, int height);
std::wstring StringToWString(const std::string &src);
std::wstring ANSIToUnicode(const std::string &src);
void ThrowJavaException(JNIEnv* env, const char* message);
jstring ToJavaString(JNIEnv* env, const std::wstring& str);
jfloatArray ToJavaArray(JNIEnv* env, const std::vector<ZXing::ResultPoint>& vector);
Expand Down
2 changes: 1 addition & 1 deletion czxing/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Java_me_devilsen_czxing_BarcodeWriter_writeCode(JNIEnv *env, jclass type, jstrin
const char *format = env->GetStringUTFChars(format_, 0);
try {
std::wstring wContent;
wContent = StringToWString(content);
wContent = ANSIToUnicode(content);

ZXing::MultiFormatWriter writer(ZXing::BarcodeFormatFromString(format));
ZXing::BitMatrix bitMatrix = writer.encode(wContent, width, height);
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/java/me/sam/czxing/WriteCodeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void writeQrCode() {

Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_avatar);

Bitmap bitmap2 = writer.write("你好,世界",
Bitmap bitmap2 = writer.write("你好,こんにちは,여보세요",
BarCodeUtil.dp2px(this, 150),
Color.parseColor("#2196F3"),
logoBitmap);
Expand Down

0 comments on commit 7658e38

Please sign in to comment.