Android ZXING barcodescan put result value in string

Android ZXING barcodescan put result value in string

我试图在扫描后获取字符串中的结果值。 但它不会工作。

我已经想出了如何将它放在一个可以工作的 toast(maketext) 中,但是我无法弄清楚一个字符串。 这是代码,请帮助我。

        cpackage com.example.stage.absa;

import android.Manifest;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.Result;
import java.io.IOException;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import android.net.Uri;
import java.net.URL;

public class MainActivity extends AbsRuntimePermission {

    String resultCode;
    private Button getBtn;
    private TextView result;
    private OkHttpClient client;
    boolean doubleTap = false;
    private ZXingScannerView scannerView;
    private static final int REQUEST_PERMISSION = 10;
    Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = (TextView) findViewById(R.id.result);
        getBtn = (Button) findViewById(R.id.getBtn);
        getBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                getWebService();
            }
        });
        client = new OkHttpClient();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        requestAppPermissions(new String[]{
                        Manifest.permission.CAMERA},
                R.string.msg, REQUEST_PERMISSION);
    }

    @Override
    public void onBackPressed() {
        if (doubleTap) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "Druk Terug opnieuw om de app te sluiten", Toast.LENGTH_SHORT).show();
            doubleTap = true;
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    doubleTap = false;
                }
            }, 500);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void browser1(View view) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.suppliance.nl/"));
        startActivity(browserIntent);
    }

    public void scanCode(View view) {
        scannerView = new ZXingScannerView(this);
        scannerView.setResultHandler(new ZxingScannerResultHandler());

        setContentView(scannerView);
        scannerView.startCamera();
    }

    class ZxingScannerResultHandler implements ZXingScannerView.ResultHandler {
        @Override
        public void handleResult(Result result) {
           // String resultCode = result.getText();
           // Toast.makeText(MainActivity.this, resultCode, Toast.LENGTH_SHORT).show();
            resultCode = result.getText();
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            scannerView.stopCamera();
        }
    }



    private void getWebService() {

        Uri.Builder builder = new Uri.Builder();
        builder.scheme("http")
                .appendPath(getString(R.string.ipadres))
                .appendPath(getString(R.string.poort))
                .authority("barcode/")
                .appendPath(getString(R.id.))
        String URL = builder.build().toString();

        final Request request = new Request.Builder().url("http://192.168.17.12:9083/barcode/8712345501006").build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        result.setText("Mislukt");
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            result.setText(response.body().string());
                        } catch (IOException ioe) {
                            result.setText("Error tijdens verkrijgen body");
                        }
                    }
                });
            }
        });
    }

    @Override
    public void onPermissionsGranted(int requestCode) {
        //Do anything when permisson granted
        Toast.makeText(getApplicationContext(), "Toegang geaccepteerd", Toast.LENGTH_LONG).show();
    }
}

删除 String 声明并将其作为 class 成员移动

 public void handleResult(Result result) {
            //String resultCode = result.getText();
             resultCode = result.getText();

喜欢:

myClass extends Activity... {
      String resultCode;
}

现在您可以在 hadnleResult 回调中分配结果代码,并在需要时在同一个 activity 中使用它!


编辑:

你有一个内部 class,我会摆脱它并直接实现 ZXingScannerView.ResultHandler 接口:

示例:

public class MainActivity extends AbsRuntimePermission  implements ZXingScannerView.ResultHandler{

    private String resultCode;
    private Button getBtn;
    private TextView result;
    private OkHttpClient client;
    boolean doubleTap = false;
    private ZXingScannerView scannerView;
    private static final int REQUEST_PERMISSION = 10;
    Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = (TextView) findViewById(R.id.result);
        getBtn = (Button) findViewById(R.id.getBtn);
        getBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                getWebService();
            }
        });
        client = new OkHttpClient();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        requestAppPermissions(new String[]{
                        Manifest.permission.CAMERA},
                R.string.msg, REQUEST_PERMISSION);
    }

    @Override
    public void onBackPressed() {
        if (doubleTap) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "Druk Terug opnieuw om de app te sluiten", Toast.LENGTH_SHORT).show();
            doubleTap = true;
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    doubleTap = false;
                }
            }, 500);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void browser1(View view) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.suppliance.nl/"));
        startActivity(browserIntent);
    }

    public void scanCode(View view) {
        scannerView = new ZXingScannerView(this);
        //scannerView.setResultHandler(new ZxingScannerResultHandler());
        scannerView.setResultHandler(this);

        setContentView(scannerView);
        scannerView.startCamera();
    }

    //class ZxingScannerResultHandler implements ZXingScannerView.ResultHandler {
        @Override
        public void handleResult(Result result) {
           // String resultCode = result.getText();
           // Toast.makeText(MainActivity.this, resultCode, Toast.LENGTH_SHORT).show();
            resultCode = result.getText();
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            scannerView.stopCamera();
        }
    //}



    private void getWebService() {

        Uri.Builder builder = new Uri.Builder();
        builder.scheme("http")
                .appendPath(getString(R.string.ipadres))
                .appendPath(getString(R.string.poort))
                .authority("barcode/")
                .appendPath(getString(R.id.))
        String URL = builder.build().toString();

        final Request request = new Request.Builder().url("http://192.168.17.12:9083/barcode/8712345501006").build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        result.setText("Mislukt");
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            result.setText(response.body().string());
                        } catch (IOException ioe) {
                            result.setText("Error tijdens verkrijgen body");
                        }
                    }
                });
            }
        });
    }

    @Override
    public void onPermissionsGranted(int requestCode) {
        //Do anything when permisson granted
        Toast.makeText(getApplicationContext(), "Toegang geaccepteerd", Toast.LENGTH_LONG).show();
    }
}

我会使用这个或类似的东西,具体取决于您处理扫描仪视图的方式

public class MainActivity extends AppCompatActivity 
                  implements ZXingScannerView.ResultHandler {

    private ZXingScannerView mScannerView;
    private String resultCode;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
        setContentView(mScannerView);                // Set the scanner view as the content view
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        resultCode = rawResult.getText();
        //now use resultCode
    }
}