Reputation: 315
So my goal is to put a barcode scanner onto my app to decode barcodes. I found a few answers using ZXing and Zbar but I either couldnt do it myself or the answer was a little out dated and half the code wouldnt work. I want to put the scanner onto a fragment and then have that 1st fragment send the decoded barcode to the 3rd fragment or results fragment. I added ZXing with intent then it prompted me to download the app and i dont want that.
Heres my MainActivity.java
package com.example.macdaddydarian.pricematch;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("Scanner");
case 1:
return SecondFragment.newInstance("Location");
case 2:
return ThirdFragment.newInstance("Results");
case 3:
return FourthFragment.newInstance("User");
default:
return ThirdFragment.newInstance("");
}
}
@Override
public int getCount() {
return 4;
}
}
}
And the FirstFragment.java that I want the scanner to go on
package com.example.macdaddydarian.pricematch;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_barcode2, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
//FONT!!!!!!!!!!!
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "Blenda Script.ttf");
tv.setTypeface(font);
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
And the fragment_barcode2.xml that goes with the first fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1ab5ff" >
<TextView
android:id="@+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="50dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_marginTop="1dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/Scanbtn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Okay and here is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.macdaddydarian.pricematch" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
And last the ThirdFragment.java i want it be sent to
package com.example.macdaddydarian.pricematch;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_results, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragThird);
tv.setText(getArguments().getString("msg"));
//FONT!!!!!!!!!!!
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"Blenda Script.ttf");
tv.setTypeface(font);
return v;
}
public static ThirdFragment newInstance(String text) {
ThirdFragment f = new ThirdFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
Upvotes: 3
Views: 2473
Reputation: 1316
Google recently released a new Android Vision Api that provides barcode functionality.
You can view sample code of the API here: https://github.com/googlesamples/android-vision
Upvotes: 1