Skip to content

Instantly share code, notes, and snippets.

@bradchesney79
Last active December 14, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradchesney79/5005374 to your computer and use it in GitHub Desktop.
Save bradchesney79/5005374 to your computer and use it in GitHub Desktop.
EDIT: No linebreaks I put into this box while typing persist. Bah.
Android ZXing Demo
You can download Eclipse from here:
http://developer.android.com/sdk/index.html
From top to bottom in Eclipse.
These are the changes I made to the cw-omnibus/Camera/ZXing project files
https://github.com/commonsguy/cw-omnibus
I did change the names of the package files by right clicking on them-- those changes are reflected in the text below (ex com.launchhouse.demo.zxing.ZXingDemo). The original names of the resources do not match the source material provided here. Right-click to make it so by renaming them in the file hierarchy pane.
I used the Android Developer site to try to not pass on incomplete information about a 'standard' Android app project file hierarchy.
http://developer.android.com/tools/projects/index.html
I also screen captured a video-- that may take some time to distill. Link forthcoming.
I want to thank everyone who showed up. I hope you were able to take something away with your for your time. Who should really be thanking you though are the next group-- which will benefit from the obvious shortcomings of the earliest times I have tried to present this subject material.
There are a lot of places for help.
I read on forums, but don't post on forums. I don't have a favorite, I google my problems first.
Then I turn to the android-dev Internet Relay Chat (IRC from way back when, yes. We nerds still use this archaic text messaging system.) channel on the freenode network. I am in a lot of channels on a regular basis and pick at random which I will help in for a bit each day.
Then you all have access to me directly. BradChesney79@gmail.com, I will do what I can. I have been given a lot of guidance, and I am starting to feel the burden that I should be giving more back. I'm not helping you for you. I'm offering help to you for me-- so don't feel bad about contacting me.
My initial raw notes follow from here, the files are in their own boxes below.
------------------------------------------------------------------------------
Install Android SDK
http://developer.android.com/sdk/index.html
Get the commonsware starter package
http://developer.android.com/sdk/index.html
// Describe the files and their significance
http://developer.android.com/tools/projects/index.html //explanation of common file locations and what they do
http://en.wikipedia.org/wiki/Java_package#Using_packages //explanation of package line inside of the main .java file
// Java OOP basics
class game {
public var noise;
private var unused;
private function bounce(thing) {
//some things are just built into the language
//we will make an assumption that we can get the noise an object makes by asking for it
output = inherentNoise(thing);
return output;
}
public function doThat(thing) {
this.noise = bounce(thing);
}
public static function doThis(thing) {
return bounce(thing);
}
}
game1 = new game;
game1.doThat(ball);
echo game1.noise; // will result in a hollow ting sound
echo game::doThis(ball); // will result in a hollow ting sound
}
// Import the commonsware camera.zxing projects example
give local download URI
provide source download URI in the notes
// For shizzles and giggles change the package name
in the res/values/strings.xml file update the app_name line: "<string name="app_name">Launch House Demo</string>"
It is worth noting that we are blatantly taking someone else's work and removing some of the indication that it was created by someone else.
change the package name in ZXingDemo\src\ZXingDemo.java from "" to "com.launchhouse.demo.zxing"
Click the containing package that contains the ZXingDemo.java then choose File>Refactor>Rename the package from "" to "com.launchhouse.demo.zxing"
// You just renamed your package!
Let's create an android virtual device.
Window>Android Virtual Device Manager
Click the 'New...' button
Now that you have a Virtual Device to play with.
Select it and start it up.
Go ahead and run your program, there is a green circle with a white 'play' triangle in it.
Click Scan!
It will ask if you want to download the latest BarcodeScanner-- you do. So, do it.
Once downloaded and you can install it. The ZXing barcodescanner.apk will be accessible via the download success notification and the downloads directory. You can use that file to install the BarcodeScanner App.
On the first usage regardless of in your app or out, the ZXing BarcodeScanner app will give you a quick initial page of information about bugfixes and/or new features along with other version information. Once closed you should see the simulated camera screen.
Let's add a button, why not.
We'll eventually send the scanned value to the server. Let's do that via the press of this new button.
Open
res/layout/main.xml
Add a button block similar to the previous button block:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="contactServer"
android:text="@string/contact" />
Let's talk about linear layout versus -----adaptive layouts-----
So we will mind where we place this block in the XML document.
Let's add a string to go with the button.
Open or switch to res/values/strings.xml and add a line as follows: "<string name="contact">Contact the server!</string>"
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
We get an error! Let's look at the LogCat.
We do not have the "INTERNET" permission, which needs declared to the user upon installation.
http://stackoverflow.com/questions/2169294/how-to-add-manifest-permission-to-android-application
<?xml version="1.0" encoding="utf-8"?>
<!-- I edited a portion of this line from "com.commonsware.android.zxing" to "com.launchhouse.demo.zxing"-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.launchhouse.demo.zxing"
android:versionCode="1"
android:versionName="1.0">
<!-- I added the <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="17" /> line to remove the warning
... and summarily took it out.
Explaining putting the chunk of code, contactServer(), that contacted the server in a background asynctask was daunting.
Having the networking in the main thread throws an error that prevents building the .apk.
For whatever reason Eclipse calls it 'building' and not 'compiling'.
-->
<uses-feature
android:name="android.hardware.camera"
android:required="true"/>
<!-- I added the permission line -->
<uses-permission
android:name="android.permission.INTERNET"/>
<application android:label="@string/app_name">
<!-- I changed this line, specifically com.launchhouse.demo.zxing -->
<activity
android:name="com.launchhouse.demo.zxing.ZXingDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<!-- res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="doScan"
android:text="@string/scan" />
<TextView
android:id="@+id/format"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF0000FF" />
<TextView
android:id="@+id/contents"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- I added these blocks -->
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FFFF0000" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="contactServer"
android:text="@string/contact" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF0000FF" />
<TextView
android:id="@+id/apiReturn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<!-- res/values/strings.xml -->
<resources>
<string name="app_name">Launch House Demo</string>
<string name="scan">Scan!</string>
<string name="contact">Contact the server!</string>
</resources>
/***
src/com.launchhouse.demo.zxing/ZXingDemo.java
Copyright (c) 2008-2010 CommonsWare, LLC
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.
*/
package com.launchhouse.demo.zxing;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
// I edited this line from "com.commonsware.android.zxing.R" to "com.launchhouse.demo.zxing.R"
import com.launchhouse.demo.zxing.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ZXingDemo extends Activity {
TextView format=null;
TextView contents=null;
// I added this
TextView apiReturn=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
format=(TextView)findViewById(R.id.format);
contents=(TextView)findViewById(R.id.contents);
// I added this line
apiReturn=(TextView)findViewById(R.id.apiReturn);
}
public void doScan(View v) {
(new IntentIntegrator(this)).initiateScan();
}
// all these lines were added to the commonsware start package START
public void onClick(View v) {
}
//*
public void contactServer(View v) {
Log.d("TAG", "To The LogCat!");
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://www.rustbeltrebellion.com/testpostscript.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("stringdata", contents.getText().toString()));
//nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode) {
case 200:
HttpEntity entity = response.getEntity();
if(entity != null) {
apiReturn.setText(EntityUtils.toString(entity));
}
break;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.d("TAG", "To The LogCat! CPE");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("TAG", "To The LogCat! IOE");
}
}
//*/
//all these lines were added to the commonsware start package END
public void onActivityResult(int request, int result, Intent i) {
IntentResult scan=IntentIntegrator.parseActivityResult(request, result, i);
if (scan!=null) {
format.setText(scan.getFormatName());
contents.setText(scan.getContents());
}
}
@Override
public void onSaveInstanceState(Bundle state) {
state.putString("format", format.getText().toString());
state.putString("contents", contents.getText().toString());
// I added this line
state.putString("apiReturn", apiReturn.getText().toString());
}
@Override
public void onRestoreInstanceState(Bundle state) {
format.setText(state.getString("format"));
contents.setText(state.getString("contents"));
// I added this line
apiReturn.setText(state.getString("apiReturn"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment