Я использую библиотеку zxing
для обнаружения штрих-кодов.
на некоторых изображениях zxing не может обнаружить штрих-код следующим образом:
, но когда sharpen filter
добавляет изображение с фотошопом, zxing
также определяет штрих-код. например:
Могу ли я добавить какой-либо фильтр на изображение до уровня обнаружения zxing?
вот мой код:
public class ScanFragment2 extends Fragment {
private ViewGroup contentFrame;
private ZXingScannerView zXingScannerView;
private ArrayList<Integer> mSelectedIndices;
private int camId, frontCamId, rearCamId;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initVar();
zXingScannerView = new ZXingScannerView(getActivity());
setupFormats();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_scan, container, false);
initView(rootView);
initListener();
return rootView;
}
private void initVar() {
camId = AppPreference.getInstance(getActivity()).getInteger(PrefKey.CAM_ID); // back camera by default
if (camId == -1) {
camId = rearCamId;
}
loadCams();
}
private void initView(View rootView) {
contentFrame = (ViewGroup) rootView.findViewById(R.id.content_frame);
}
private void initListener() {
zXingScannerView.setResultHandler(new ZXingScannerView.ResultHandler() {
@Override
public void handleResult(Result result) {
String resultStr = result.getText();
//this is my result: resultStr
zXingScannerView.resumeCameraPreview(this);
}
});
}
private void activateScanner() {
if (zXingScannerView != null) {
if (zXingScannerView.getParent() != null) {
((ViewGroup) zXingScannerView.getParent()).removeView(zXingScannerView); // to prevent crush on re adding view
}
contentFrame.addView(zXingScannerView);
if (zXingScannerView.isActivated()) {
zXingScannerView.stopCamera();
}
zXingScannerView.startCamera(camId);
}
}
public void setupFormats() {
List<BarcodeFormat> formats = new ArrayList<>();
if (mSelectedIndices == null || mSelectedIndices.isEmpty()) {
mSelectedIndices = new ArrayList<>();
for (int i = 0; i < ZXingScannerView.ALL_FORMATS.size(); i++) {
mSelectedIndices.add(i);
}
}
for (int index : mSelectedIndices) {
formats.add(ZXingScannerView.ALL_FORMATS.get(index));
}
if (zXingScannerView != null) {
zXingScannerView.setFormats(formats);
}
}
@Override
public void onResume() {
super.onResume();
activateScanner();
}
@Override
public void onPause() {
super.onPause();
if (zXingScannerView != null) {
zXingScannerView.stopCamera();
}
}
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (zXingScannerView != null) {
zXingScannerView.setFlash(false);
}
}
private void toggleCamera() {
if (camId == rearCamId) {
camId = frontCamId;
} else {
camId = rearCamId;
}
AppPreference.getInstance(getActivity()).setInteger(PrefKey.CAM_ID, camId);
zXingScannerView.stopCamera();
zXingScannerView.startCamera(camId);
}
private void loadCams() {
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
frontCamId = i;
} else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
rearCamId = i;
}
}
AppPreference.getInstance(getActivity()).setInteger(PrefKey.CAM_ID, rearCamId);
}
}
и пользовательский интерфейс:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>