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

标签 java android android-fragments barcode barcode-scanner

所以我的目标是在我的应用程序中放置一个条形码扫描器来解码条形码。我使用 ZXing 和 Zbar 找到了一些答案,但要么我自己做不到,要么答案有点过时,一半的代码不起作用。我想将扫描仪放在一个 fragment 上,然后让第一个 fragment 将解码后的条形码发送到第三个 fragment 或结果 fragment 。我有意添加了 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 一起使用的 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;
}
}

最佳答案

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

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

关于java - 通过android studio将嵌入式条形码扫描仪添加到 fragment 到我的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706112/

相关文章:

java - 如何防止可运行 JAR 中出现 "No jhdf5 in java.library.path"错误

java - 无法从静态上下文引用非静态方法 "getActivity"

android - 将 fragment 添加到操作栏中

java - Android - 创建一个类的实例并从 String 调用

android - 在不导致 illegalStateException 的情况下弹出返回堆栈

java - 如何在运行时更改 persistence.xml

java - 找不到方法 setModal(boolean)

java - 使用反射时处理或转换从 method.invoke() 返回的对象

android - 如何从我的 Android 应用程序将图像分享到 Snapchat?

Android 如何保护 mp3 文件不被共享、复制和出现在其他音乐播放器中