通过android studio将嵌入式条形码扫描器添加到我的应用程序中

时间:2015-09-21 23:32:39

标签: java android android-fragments barcode barcode-scanner

所以我的目标是将条形码扫描仪放到我的应用程序上以解码条形码。我使用ZXing和Zbar找到了一些答案,但我要么自己做不到,要么答案有点过时,一半的代码也行不通。我想将扫描仪放到一个片段上然后让第一个片段将解码的条形码发送到第三个片段或结果片段。我添加ZXing意图然后它促使我下载应用程序,我不想要那个。

继承我的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;
    }
}

}

我希望扫描仪继续使用的FirstFragment.java

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;
 }
}

与第一个片段

一起使用的fragment_barcode2.xml
<?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>

好的,这是我的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>

最后我希望将它发送到

的ThirdFragment.java
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;
}
}

1 个答案:

答案 0 :(得分:1)

Google最近发布了一个提供条形码功能的新Android Vision Api

您可以在此处查看API的示例代码:https://github.com/googlesamples/android-vision