Creating a Barcode Scanner using Android Studio
Step by step guide to building a barcode scanning application using Android Studio.
What is this article about
This article will guide you through creating an android application using which you can make use of your mobile camera to scan a barcode and read the data in them.
Prerequisite
- The latest version of Android Studio installed. ( download from here )
- A mobile device to test the application. (you can make use of the inbuild android emulator but in some pc, it may cause some issues.)
- Knowledge of java or any object-oriented programing language.
Let’s write some code
- Create a new application in android studio and name it Barcode Scanner.
- Open up your app-level Gradle file and add the below dependency there.
3. Now hit on Sync now button and wait for the build to complete. Once the build is complete open up your manifest file and add the necessary permissions.
also, add the metadata field in your manifest file, inside the application tag and above the activity tag.
here’s a full view of my manifest file
4. Now you have set up all the dependency needed for the barcode scanner to work and all the permission necessary. Let’s build the UI for the app.
5. Open your activity_main.xml file and write the below code inside.
Now your view should look something like this
here we have something called a sufaceview in android and a textview field to display the text scanned by the barcode.
SurfaceView: It provides a dedicated drawing surface embedded inside the view hierarchy.
You have completed the UI code for the Barcode App, now let’s write the java code to make wonders happen.
6. Open MainActivity.java file and you will see the following code.
let’s add some code of our own
7. First, we need to bind the views.
8. Now we will write the method to scan the image for a barcode.
this method will help us scan and display the text in the textview we created in the XML file.
9. The complete java code will look something like this.
Now try scanning this barcode and you will see the value that is embedded in the barcode in the textview.
The final screen should something like this.
Источник
Как читать и писать UTF-8 на диск на Android?
Я не могу читать и писать расширенные символы (например, французские акцентированные символы) в текстовый файл, используя стандартные методы InputStreamReader, показанные в примерах API Android. Когда я читаю файл, используя:
Чтение строки усекается расширенными символами, а не в конце строки. Вторая половина строки появляется на следующей строке. Я предполагаю, что мне нужно сохранить мои данные как UTF-8, но я не могу найти примеров этого, и я новичок в Java.
Может ли кто-нибудь предоставить мне пример или ссылку на соответствующую документацию?
Очень простой и понятный. 🙂
Когда вы создаете экземпляр InputStreamReader , используйте конструктор, который принимает набор символов.
И сделать аналогичную вещь с OutputStreamWriter
В каком-то классе утилиты в моем коде, чтобы я мог позвонить (см. Больше в Doc )
И не нужно обрабатывать UnsupportedEncodingException каждый раз.
Это должно просто работать на Android, даже без явного указания UTF-8, потому что кодировка по умолчанию – UTF-8. Если вы можете воспроизвести эту проблему, пожалуйста, поднимите ошибку с воспроизводимым тестовым примером здесь:
Если вы столкнулись с такой проблемой, попробуйте это сделать. Вы должны Decode и Decode свои данные в Base64 . Это сработало для меня. Я могу поделиться кодом, если вам это нужно.
Проверьте кодировку файла, щелкнув его правой кнопкой мыши в Project Explorer и выбрав свойства. Если это неправильная кодировка, вам нужно будет повторно ввести специальные символы после ее изменения, или, по крайней мере, это был мой опыт.
Источник