没有结果的ZXing条形码阅读器

时间:2014-03-18 09:14:19

标签: java android bitmap zxing

我有一个只有CameraPreviewbutton的简单Android应用程序。 它应拍照,将其转换为BinaryBitmap,然后对其进行解码,以便我可以读取条形码。我在java中使用 ZXing library version 2.3 Android Studio版本0.5.1 我甚至不知道我是否以正确的方式拍摄bitmap照片。但最后,当我尝试解码NotFoundException时,我只得到bitmap

result = barcodeReader.decode(bMap);

这是我的MainActivity.class

public class MainActivity extends Activity {
    private Camera mCamera;
    private CameraPreview mPreview;
    private Bitmap mBitmap;

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        load();
    }

    private void load()
    {
        // Create instance of Camera
        mCamera = getCameraInstance();
        mCamera.setDisplayOrientation(90);

        // get Camera parameters
        Camera.Parameters params = mCamera.getParameters();
        // set the focus mode
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        // set Camera parameters
        mCamera.setParameters(params);


        // Create our Preview view and set it as the content of our activity
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    /** Check if this device has a camera */
    private boolean checkCameraHardware(Context context) {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

    public void takeSnapshot(View view)
    {
        Camera.AutoFocusCallback focusCallback = new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean b, Camera camera) {

                Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] bytes, Camera camera) {
                        Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        mBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
                        bmp = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth()/2, mBitmap.getHeight()/2, true);

                        String text = "";
                        try
                        {
                            text = decode(bmp, null);
                        }
                        catch(Exception e) { }
                        Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();

                        mCamera.startPreview();
                    }
                };

                mCamera.takePicture(null, null, jpegCallback);
            }
        };

        mCamera.autoFocus(focusCallback);
    }

    public static String decode(Bitmap bitmap, Map<DecodeHintType, Object> hints) throws Exception {
        // check the required parameters

        int[] intArray = new int[bitmap.getWidth()*bitmap.getHeight()];
        LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
        BinaryBitmap bMap = new BinaryBitmap(new HybridBinarizer(source));
        MultiFormatReader barcodeReader = new MultiFormatReader();
        Result result = null;

        try {
            if (hints != null && ! hints.isEmpty())
                result = barcodeReader.decode(bMap, hints);
            else
                result = barcodeReader.decode(bMap);
            // setting results.
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(result != null)
            return String.valueOf(result.getText());
        else
            return "No code found.";
    }
}

0 个答案:

没有答案