Code Monkey home page Code Monkey logo

simplecamerareader-android's Introduction

How to use:

First build the project and reference the jar (or project) in your Android project. Then in your Android project create the layout for the scan-view which can be as easy as:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <FrameLayout
        android:id="@+id/cameraview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    </FrameLayout>
</FrameLayout>

In your AndroidManifest.xml you will need to allow camera-use:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Then in your scan-activity you can use it as simply as:

public class ScanActivity extends Activity
{
	private SimpleCameraReader camera;

	private Context context;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.scan_layout);

		try
		{
			context = this;

			// We create a collection of formats we want to use
			Collection<BarcodeFormat> formats = new ArrayList<BarcodeFormat>();

			// Which in this case is EAN codes (European Product codes)
			formats.add(BarcodeFormat.EAN_13);
			formats.add(BarcodeFormat.EAN_8);

			// Initialize camera object with context and formats
			camera = new SimpleCameraReader(this, formats);

			// Here we add the CameraView to our FrameLayout
			((FrameLayout) findViewById(R.id.cameraview)).addView(camera
					.getCameraView());

			// Make the view transparent so widgets/views in front of our cameraview
			// shows
			camera.getCameraView().setBackgroundColor(Color.TRANSPARENT);

			// We add a ResultHandler which will take care of an eventual
			// result or errors
			camera.setResultHandler(new CameraHandler()
			{

				// When decoding completes and we found a barcode
				// this method is called with a Result-object
				public void onDecodeCompleted(Result result)
				{
					Toast.makeText(context, "Found barcode: " + result.getText().toString(), Toast.LENGTH_LONG).show();
				}

				// In case of an error, this method is called
				// We don't really have to do anything here
				public void onError(Exception ex)
				{

				}
			});
		}
		catch (Exception ex)
		{

		}
	}

	public void onResume()
	{
		super.onResume();

		// When the activity resumes/starts
		// we'll start the camera
		try
		{
			camera.start();
		}
		catch (Exception ex)
		{

		}
	}

	public void onPause()
	{
		super.onPause();

		// When the activity pauses/stops
		// stop the camera
		camera.stop();
	}
}

To use it as a maven dependency, just add

<groupId>no.henning</groupId>
<artifactId>simple-camera-reader</artifactId>
<version>1.0</version>
<type>apklib</type>

To your pom.


Copyright [2012, 2013] [Henning Mosand Stephansen]

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.

simplecamerareader-android's People

Contributors

henningms avatar simenb avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.