Implement zbar scanner in alredy existing layout

2013-08-20
2013-08-20
  • m trting to place camera previwe of zbar scanner to my existing layout, but the place where Im trying to put it always stays black and dosn't start the camera to read qrs. If I dont use the custom layout but the one thats in the example(it starts scanner in all new activity and it takes all the screen size) everything is fine. This means I have no problems with permissions or something like this. This is the codes im using now:

    import net.sourceforge.zbar.Config;
    import net.sourceforge.zbar.Image;
    import net.sourceforge.zbar.ImageScanner;
    import net.sourceforge.zbar.Symbol;
    import net.sourceforge.zbar.SymbolSet;

    import com.dm.zbar.android.scanner.CameraPreview;
    import com.dm.zbar.android.scanner.ZBarConstants;
    import com.dm.zbar.android.scanner.ZBarScannerActivity;

    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.hardware.Camera;
    import android.text.TextUtils;
    import android.view.Menu;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.LinearLayout;
    import android.widget.LinearLayout.LayoutParams;
    import android.widget.TextView;
    import android.widget.Toast;

    public class PointGivingActivity extends Activity implements Camera.PreviewCallback, ZBarConstants{

    private static final int ZBAR_SCANNER_REQUEST = 0;
    private static final int ZBAR_QR_SCANNER_REQUEST = 1;
    private static final String TAG = "ZBarScannerActivity";
    private CameraPreview mPreview;
    private Camera mCamera;
    private ImageScanner mScanner;
    private Handler mAutoFocusHandler;
    private boolean mPreviewing = true;
    static {
        System.loadLibrary("iconv");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isCameraAvailable()) {
            cancelRequest();
            return;
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_point_giving2);
        mAutoFocusHandler = new Handler();
        setupScanner();
    
        LinearLayout zbarLayout = (LinearLayout) findViewById(R.id.zbar_layout_area);
    
        mPreview = new CameraPreview(zbarLayout.getContext(), this, autoFocusCB);
        mPreview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        zbarLayout.addView(mPreview);
    }
    
    public void launchScanner(View v) {
        if (isCameraAvailable()) {
            Intent intent = new Intent(this, ZBarScannerActivity.class);
            startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
        } else {
            Toast.makeText(this, "Rear Facing Camera Unavailable",
                    Toast.LENGTH_SHORT).show();
        }
    }
    public void launchScanner() {
        if (isCameraAvailable()) {
            Intent intent = new Intent(this, ZBarScannerActivity.class);
            startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
        } else {
            Toast.makeText(this, "Rear Facing Camera Unavailable",
                    Toast.LENGTH_SHORT).show();
        }
    }
    public void launchQRScanner(View v) {
        if (isCameraAvailable()) {
            Intent intent = new Intent(this, ZBarScannerActivity.class);
            intent.putExtra(ZBarConstants.SCAN_MODES,
                    new int[] { Symbol.QRCODE });
            startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
        } else {
            Toast.makeText(this, "Rear Facing Camera Unavailable",
                    Toast.LENGTH_SHORT).show();
        }
    }
    
    public void MyMethod(View v) {
        Intent intent = new Intent(this, ZBarScannerActivity.class);
        startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            TextView someText = (TextView) findViewById(R.id.givePointtv1);
            someText.setText(data.getStringExtra(ZBarConstants.SCAN_RESULT));
            // Toast.makeText(this, "Scan Result = " +
            // data.getStringExtra(ZBarConstants.SCAN_RESULT),
            // Toast.LENGTH_SHORT).show();
            // Toast.makeText(this, "Scan Result Type = " +
            // data.getStringExtra(ZBarConstants.SCAN_RESULT_TYPE),
            // Toast.LENGTH_SHORT).show();
    
        } else if (resultCode == RESULT_CANCELED) {
            // Toast.makeText(this, "Camera unavailable",
            // Toast.LENGTH_SHORT).show();
        }
    }
    
    public boolean isCameraAvailable() {
        PackageManager pm = getPackageManager();
        return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
    }
    
    public void cancelRequest() {
        Intent dataIntent = new Intent();
        dataIntent.putExtra(ERROR_INFO, "Camera unavailable");
        setResult(Activity.RESULT_CANCELED, dataIntent);
        finish();
    }
    
    public void onPreviewFrame(byte[] data, Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = parameters.getPreviewSize();
    
        Image barcode = new Image(size.width, size.height, "Y800");
        barcode.setData(data);
    
        int result = mScanner.scanImage(barcode);
    
        if (result != 0) {
            mCamera.cancelAutoFocus();
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mPreviewing = false;
            SymbolSet syms = mScanner.getResults();
            for (Symbol sym : syms) {
                String symData = sym.getData();
                if (!TextUtils.isEmpty(symData)) {
                    Intent dataIntent = new Intent();
                    dataIntent.putExtra(SCAN_RESULT, symData);
                    dataIntent.putExtra(SCAN_RESULT_TYPE, sym.getType());
                    setResult(Activity.RESULT_OK, dataIntent);
                    finish();
                    break;
                }
            }
        }
    }
    private Runnable doAutoFocus = new Runnable() {
        public void run() {
            if(mCamera != null && mPreviewing) {
                mCamera.autoFocus(autoFocusCB);
            }
        }
    };
    
    // Mimic continuous auto-focusing
    Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
            mAutoFocusHandler.postDelayed(doAutoFocus, 1000);
        }
    };
    public void setupScanner() {
        mScanner = new ImageScanner();
        mScanner.setConfig(0, Config.X_DENSITY, 3);
        mScanner.setConfig(0, Config.Y_DENSITY, 3);
    
        int[] symbols = getIntent().getIntArrayExtra(SCAN_MODES);
        if (symbols != null) {
            mScanner.setConfig(Symbol.NONE, Config.ENABLE, 0);
            for (int symbol : symbols) {
                mScanner.setConfig(symbol, Config.ENABLE, 1);
            }
        }
    }
    

    }
    Im pretty sure the problem is here somewhere:

    LinearLayout zbarLayout = (LinearLayout) findViewById(R.id.zbar_layout_area);

    mPreview = new CameraPreview(zbarLayout.getContext(), this, autoFocusCB);
    mPreview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    zbarLayout.addView(mPreview);
    But cant figure out how to make it work the way I want.

    Here is and my xml file if is in need

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pointgivingll1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1f1f1" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:weightSum="10">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="8" >
    
        <Button
            android:id="@+id/givePointMenuBut"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="invisible" />
    
        <TextView
            android:id="@+id/givePointtv1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="6"
            android:gravity="center"
            android:text="@string/pointGiving"
            android:textColor="@android:color/black"
            android:textSize="22sp" />
    
        <Button
            android:id="@+id/givePointMenuBut"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/ruchno"
            android:textSize="10sp" />
    </LinearLayout>
    
    <LinearLayout
        android:gravity="center"
        android:layout_weight="9"
        android:id="@+id/zbar_layout_area"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical" >
    </LinearLayout>
    

    </LinearLayout>