Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsen committed Apr 24, 2023
1 parent 622bb32 commit e3e5e63
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 12 deletions.
95 changes: 94 additions & 1 deletion czxing/src/main/cpp/ScanResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,102 @@ jintArray getJavaArray(JNIEnv *env, const czxing::CodeRect& codeRect)
return array;
}

inline int UTF82UnicodeOne(const char* utf8, wchar_t& wch)
{
//首字符的Ascii码大于0xC0才需要向后判断,否则,就肯定是单个ANSI字符了
unsigned char firstCh = utf8[0];
if (firstCh >= 0xC0)
{
//根据首字符的高位判断这是几个字母的UTF8编码
int afters, code;
if ((firstCh & 0xE0) == 0xC0)
{
afters = 2;
code = firstCh & 0x1F;
}
else if ((firstCh & 0xF0) == 0xE0)
{
afters = 3;
code = firstCh & 0xF;
}
else if ((firstCh & 0xF8) == 0xF0)
{
afters = 4;
code = firstCh & 0x7;
}
else if ((firstCh & 0xFC) == 0xF8)
{
afters = 5;
code = firstCh & 0x3;
}
else if ((firstCh & 0xFE) == 0xFC)
{
afters = 6;
code = firstCh & 0x1;
}
else
{
wch = firstCh;
return 1;
}

//知道了字节数量之后,还需要向后检查一下,如果检查失败,就简单的认为此UTF8编码有问题,或者不是UTF8编码,于是当成一个ANSI来返回处理
for(int k = 1; k < afters; ++ k)
{
if ((utf8[k] & 0xC0) != 0x80)
{
//判断失败,不符合UTF8编码的规则,直接当成一个ANSI字符返回
wch = firstCh;
return 1;
}

code <<= 6;
code |= (unsigned char)utf8[k] & 0x3F;
}

wch = code;
return afters;
}
else
{
wch = firstCh;
}

return 1;
}

int UTF82Unicode(const char* utf8Buf, wchar_t *pUniBuf, int utf8Leng)
{
int i = 0, count = 0;
while(i < utf8Leng)
{
i += UTF82UnicodeOne(utf8Buf + i, pUniBuf[count]);
count ++;
}

return count;
}

jstring stringTojstring(JNIEnv* env, const std::string& str)
{
int len = str.length();
wchar_t *wcs = new wchar_t[len * 2];
int nRet = UTF82Unicode(str.c_str(), wcs, len);
jchar* jcs = new jchar[nRet];
for (int i = 0; i < nRet; i++)
{
jcs[i] = (jchar) wcs[i];
}

jstring retString = env->NewString(jcs, nRet);
delete[] wcs;
delete[] jcs;
return retString;
}

jobject ScanResult::getJCodeResult(JNIEnv* env)
{
auto text = env->NewStringUTF(m_text.c_str());
auto text = stringTojstring(env, m_text);
auto format = static_cast<int>(m_codeFormat);
return env->NewObject(j_codeResult.clazz, j_codeResult.builder, text, format, getJavaArray(env, rect()), 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void init(Context context) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// mCamera = new ScanCamera2(context, mCameraSurface);
// } else {
mCamera = new ScanCamera1(context, mCameraSurface);
mCamera = new ScanCamera1(context, mCameraSurface);
// }
mCamera.onCreate();
mCamera.setPreviewListener(this);
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ android {
dependencies {

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
// implementation project(':czxing')
implementation 'io.github.devilsen:czxing:1.2.0'
implementation project(':czxing')
// implementation 'io.github.devilsen:czxing:1.2.0'

testImplementation rootProject.ext.junit
androidTestImplementation rootProject.ext.testrunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.List;

import me.devilsen.czxing.code.BarcodeDecoder;
import me.devilsen.czxing.code.CodeResult;
import me.devilsen.czxing.compat.ActivityCompat;
import me.devilsen.czxing.compat.ContextCompat;
import me.devilsen.czxing.util.AssetUtil;
import me.devilsen.czxing.util.BitmapUtil;
import me.devilsen.czxing.util.ScreenUtil;
import me.devilsen.czxing.util.SoundPoolUtil;
import me.devilsen.czxing.util.SoundPlayer;
import me.devilsen.czxing.view.scanview.ScanBoxView;
import me.devilsen.czxing.view.scanview.ScanLayout;
import me.devilsen.czxing.view.scanview.ScanListener;
Expand All @@ -50,7 +50,7 @@ public class CustomizeActivity extends AppCompatActivity implements View.OnClick
private static final int CODE_SELECT_IMAGE = 100;

private ScanLayout mScanLayout;
private SoundPoolUtil mSoundPoolUtil;
private SoundPlayer mSoundPool;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -121,8 +121,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) titleLayout.getLayoutParams();
layoutParams.topMargin = ScreenUtil.getStatusBarHeight(this);

mSoundPoolUtil = new SoundPoolUtil();
mSoundPoolUtil.loadDefault(this);
mSoundPool = new SoundPlayer();
mSoundPool.loadDefault(this);

requestCameraPermission();

Expand Down Expand Up @@ -150,7 +150,7 @@ protected void onPause() {
@Override
protected void onDestroy() {
mScanLayout.onDestroy(); // 销毁二维码扫描控件
mSoundPoolUtil.release();
mSoundPool.release();
super.onDestroy();
}

Expand Down Expand Up @@ -190,7 +190,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

@Override
public void onScanSuccess(@NonNull List<CodeResult> resultList) {
mSoundPoolUtil.play();
mSoundPool.play();

// todo deal with results
// showResult(result);
Expand Down

0 comments on commit e3e5e63

Please sign in to comment.