forked from zxing-cpp/zxing-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for WebAssemly using Emscripten
- Loading branch information
Showing
8 changed files
with
9,378 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Copyright (c) 2005-2018 Lode Vandevenne | ||
|
||
This software is provided 'as-is', without any express or implied | ||
warranty. In no event will the authors be held liable for any damages | ||
arising from the use of this software. | ||
|
||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute it | ||
freely, subject to the following restrictions: | ||
|
||
1. The origin of this software must not be misrepresented; you must not | ||
claim that you wrote the original software. If you use this software | ||
in a product, an acknowledgment in the product documentation would be | ||
appreciated but is not required. | ||
|
||
2. Altered source versions must be plainly marked as such, and must not be | ||
misrepresented as being the original software. | ||
|
||
3. This notice may not be removed or altered from any source | ||
distribution. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2016 Nu-book Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "GenericLuminanceSource.h" | ||
#include "HybridBinarizer.h" | ||
#include "MultiFormatReader.h" | ||
#include "Result.h" | ||
#include "DecodeHints.h" | ||
#include "BarcodeFormat.h" | ||
#include "lodepng.h" | ||
|
||
#include <string> | ||
#include <memory> | ||
#include <emscripten/bind.h> | ||
|
||
#if 0 | ||
using Binarizer = ZXing::GlobalHistogramBinarizer; | ||
#else | ||
using Binarizer = ZXing::HybridBinarizer; | ||
#endif | ||
|
||
struct ReadResult | ||
{ | ||
std::string format; | ||
std::wstring text; | ||
std::string error; | ||
}; | ||
|
||
ReadResult readBarcodeFromPng(int bufferPtr, int bufferLength, bool tryHarder, std::string format) | ||
{ | ||
using namespace ZXing; | ||
try { | ||
DecodeHints hints; | ||
hints.setShouldTryHarder(tryHarder); | ||
hints.setShouldTryRotate(tryHarder); | ||
auto fixedFormat = BarcodeFormatFromString(format); | ||
if (fixedFormat != BarcodeFormat::FORMAT_COUNT) | ||
hints.setPossibleFormats({ fixedFormat }); | ||
MultiFormatReader reader(hints); | ||
|
||
std::vector<unsigned char> bitmap; | ||
unsigned width, height; | ||
unsigned error = lodepng::decode(bitmap, width, height, reinterpret_cast<const unsigned char*>(bufferPtr), size_t(bufferLength)); | ||
if (error) { | ||
return { "", L"", lodepng_error_text(error) }; | ||
} | ||
|
||
GenericLuminanceSource source((int)width, (int)height, bitmap.data(), width * 4, 4, 0, 1, 2); | ||
Binarizer binImage(std::shared_ptr<LuminanceSource>(&source, [](void*) {})); | ||
|
||
auto result = reader.read(binImage); | ||
if (result.isValid()) { | ||
return { ToString(result.format()), result.text(), "" }; | ||
} | ||
} | ||
catch (const std::exception& e) { | ||
return { "", L"", e.what() }; | ||
} | ||
catch (...) { | ||
|
||
} | ||
return {}; | ||
} | ||
|
||
EMSCRIPTEN_BINDINGS(XZing) | ||
{ | ||
using namespace emscripten; | ||
|
||
value_object<ReadResult>("ReadResult") | ||
.field("format", &ReadResult::format) | ||
.field("text", &ReadResult::text) | ||
.field("error", &ReadResult::error) | ||
; | ||
|
||
function("readBarcodeFromPng", &readBarcodeFromPng); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
cmake_minimum_required (VERSION 3.1.3) | ||
|
||
project (ZXingWasm) | ||
|
||
set (ENABLE_ENCODERS ON CACHE INTERNAL "Check to include encoders") | ||
set (ENABLE_DECODERS ON CACHE INTERNAL "Check to include decoders") | ||
|
||
add_definitions (-DUNICODE -D_UNICODE) | ||
|
||
if (MSVC) | ||
add_definitions (-D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -DNOMINMAX) | ||
|
||
set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Oi /GS-") | ||
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Oi /GS-") | ||
else() | ||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") | ||
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG") | ||
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG") | ||
endif() | ||
|
||
include (CheckCXXCompilerFlag) | ||
CHECK_CXX_COMPILER_FLAG ("-std=c++11" COMPILER_SUPPORTS_CXX11) | ||
|
||
if (COMPILER_SUPPORTS_CXX11) | ||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
endif() | ||
|
||
add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR}/../../core ${CMAKE_BINARY_DIR}/ZXingCore) | ||
|
||
add_executable (zxing | ||
BarcodeReader.cpp | ||
lodepng.cpp | ||
) | ||
|
||
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --bind") | ||
|
||
target_link_libraries (zxing ZXingCore) |
Oops, something went wrong.