Question About QR code scanner
Opened this issue · 20 comments
I Tried to implement the QR code scanner copied and pasted everything you got in the zxing package
however when I try to scan a QR code it doesn't work.
in addition to that I copied and pasted the code In the AddPackageFragment
for scanning and getting the activity result and still won't work.
Hi, @houcem365.
Have you added the dependency of zxing-core to your build.gradle(app level) yet?
compile 'com.google.zxing:core:3.3.0'
After that, you also need to paste the resource files such the images(in res
- drawable
) and others.
If you get any other problems, do not hesitate to edit the issue. I will try my best to help.
This is another project that I regard it as a reference: SearchItem, but you know, it is in Chinese, so...
But if the code is not difficult to understand.
Can I have a glance at your code? That may help me to find the bug more easier.
Okay this is the permission in the manifest file:
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.flash"
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
And this is the CaptureActivity
:
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.google.zxing.Result;
import project.selim.com.mo.R;
import project.selim.com.mo.zxing.camera.CameraManager;
import project.selim.com.mo.zxing.decode.DecodeThread;
import project.selim.com.mo.zxing.utils.BeepManager;
import project.selim.com.mo.zxing.utils.CaptureActivityHandler;
import project.selim.com.mo.zxing.utils.InactivityTimer;
/**
* Created by lizhaotailang on 2017/2/13.
*/
public class CaptureActivity extends AppCompatActivity
implements SurfaceHolder.Callback {
private static final String TAG = CaptureActivity.class.getSimpleName();
private CameraManager cameraManager;
private CaptureActivityHandler handler;
private InactivityTimer inactivityTimer;
private BeepManager beepManager;
private SurfaceView scanPreview = null;
private RelativeLayout scanContainer;
private RelativeLayout scanCropView;
private ImageView scanLine;
private Rect mCropRect = null;
public Handler getHandler() {
return handler;
}
public CameraManager getCameraManager() {
return cameraManager;
}
private boolean isHasSurface = false;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_scan);
scanPreview = (SurfaceView) findViewById(R.id.capture_preview);
scanContainer = (RelativeLayout) findViewById(R.id.capture_container);
scanCropView = (RelativeLayout) findViewById(R.id.capture_crop_view);
scanLine = (ImageView) findViewById(R.id.capture_scan_line);
inactivityTimer = new InactivityTimer(this);
beepManager = new BeepManager(this);
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.9f);
animation.setDuration(4500);
animation.setRepeatCount(-1);
animation.setRepeatMode(Animation.RESTART);
scanLine.startAnimation(animation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onResume() {
super.onResume();
// CameraManager must be initialized here, not in onCreate(). This is
// necessary because we don't
// want to open the camera driver and measure the screen size if we're
// going to show the help on
// first launch. That led to bugs where the scanning rectangle was the
// wrong size and partially
// off screen.
cameraManager = new CameraManager(getApplication());
handler = null;
if (isHasSurface) {
// The activity was paused but not stopped, so the surface still
// exists. Therefore
// surfaceCreated() won't be called, so init the camera here.
initCamera(scanPreview.getHolder());
} else {
// Install the callback and wait for surfaceCreated() to init the
// camera.
scanPreview.getHolder().addCallback(this);
}
inactivityTimer.onResume();
}
@Override
protected void onPause() {
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
inactivityTimer.onPause();
beepManager.close();
cameraManager.closeDriver();
if (!isHasSurface) {
scanPreview.getHolder().removeCallback(this);
}
super.onPause();
}
@Override
protected void onDestroy() {
inactivityTimer.shutdown();
super.onDestroy();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
return true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (holder == null) {
Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
}
if (!isHasSurface) {
isHasSurface = true;
initCamera(holder);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isHasSurface = false;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
/**
* A valid barcode has been found, so give an indication of success and show
* the results.
*
* @param rawResult The contents of the barcode.
* @param bundle The extras
*/
public void handleDecode(Result rawResult, Bundle bundle) {
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
Intent resultIntent = new Intent();
bundle.putInt("width", mCropRect.width());
bundle.putInt("height", mCropRect.height());
bundle.putString("result", rawResult.getText());
resultIntent.putExtras(bundle);
setResult(RESULT_OK,resultIntent);
finish();
}
/**
* Init the camera.
* @param surfaceHolder The surface holder.
*/
private void initCamera(SurfaceHolder surfaceHolder) {
if (surfaceHolder == null) {
throw new IllegalStateException("No SurfaceHolder provided");
}
if (cameraManager.isOpen()) {
Log.w(TAG, "initCamera() while already open -- late SurfaceView callback?");
return;
}
try {
cameraManager.openDriver(surfaceHolder);
// Creating the handler starts the preview, which can also throw a
// RuntimeException.
if (handler == null) {
handler = new CaptureActivityHandler(this, cameraManager, DecodeThread.ALL_MODE);
}
initCrop();
} catch (IOException ioe) {
Log.w(TAG, ioe);
displayFrameworkBugMessageAndExit();
} catch (RuntimeException e) {
// Barcode Scanner has seen crashes in the wild of this variety:
// java.?lang.?RuntimeException: Fail to connect to camera service
Log.w(TAG, "Unexpected error initializing camera", e);
displayFrameworkBugMessageAndExit();
}
}
private void displayFrameworkBugMessageAndExit() {
// camera error
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setMessage(getString(R.string.unable_to_open_camera));
dialog.setTitle(getString(R.string.error));
dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(android.R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
public void restartPreviewAfterDelay(long delayMS) {
if (handler != null) {
handler.sendEmptyMessageDelayed(R.id.restart_preview, delayMS);
}
}
public Rect getCropRect() {
return mCropRect;
}
/**
* Init the interception rectangle area
*/
private void initCrop() {
int cameraWidth = cameraManager.getCameraResolution().y;
int cameraHeight = cameraManager.getCameraResolution().x;
// Obtain the location information of the scanning frame in layout
int[] location = new int[2];
scanCropView.getLocationInWindow(location);
int cropLeft = location[0];
int cropTop = location[1] - getStatusBarHeight();
int cropWidth = scanCropView.getWidth();
int cropHeight = scanCropView.getHeight();
// Obtain the height and width of layout container.
int containerWidth = scanContainer.getWidth();
int containerHeight = scanContainer.getHeight();
// Compute the coordinate of the top-left vertex x
// of the final interception rectangle.
int x = cropLeft * cameraWidth / containerWidth;
// Compute the coordinate of the top-left vertex y
// of the final interception rectangle.
int y = cropTop * cameraHeight / containerHeight;
// Compute the width of the final interception rectangle.
int width = cropWidth * cameraWidth / containerWidth;
// Compute the height of the final interception rectangle.
int height = cropHeight * cameraHeight / containerHeight;
// Generate the final interception rectangle.
mCropRect = new Rect(x, y, width + x, height + y);
}
private int getStatusBarHeight() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
And this is the listener and onActivityResult
findViewById(R.id.zxing).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startScanningActivity();
}
});
String action = getIntent().getAction();
if (action != null && action.equals(ACTION_SCAN_CODE)) {
checkPermissionOrToScan();
}
}
private void startScanningActivity() {
try {
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, SCANNING_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
/**
* Check whether the camera permission has been granted.
* If not, request it. Or just launch the camera to scan barcode or QR code.
*/
private void checkPermissionOrToScan() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Notice: Do not use the below code.
// ActivityCompat.requestPermissions(getActivity(),
// new String[] {Manifest.permission.CAMERA}, 1);
// Such code may still active the request permission dialog
// but even the user has granted the permission,
// app will response nothing.
// The below code works perfect.
requestPermissions(new String[] { Manifest.permission.CAMERA }, REQUEST_CAMERA_PERMISSION_CODE);
} else {
startScanningActivity();
}
}
/*@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents() == null){
Toast.makeText(this, "you cancelled the scan", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}else{
super.onActivityResult(requestCode, resultCode, data);
}
}*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v("MainActivity", String.valueOf(requestCode) );
switch (requestCode) {
case SCANNING_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
if (null != bundle) {
Toast.makeText(this, bundle.getString("result"), Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(this, "Cancel" , Toast.LENGTH_SHORT).show();
}
}
break;
default:
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA_PERMISSION_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startScanningActivity();
} else {
// hideImm();
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.permission_denied)
.setMessage(R.string.require_permission)
.setPositiveButton("FOOk", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Go to the detail settings of our application
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
dialog.dismiss();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
dialog.show();
}
break;
default:
}
}
@houcem365 How about adding the code below to your manifest?
<activity
android:name=".zxing.CaptureActivity"
android:label="@string/activity_scan_code"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="io.github.marktony.espresso.zxing.CaptureActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If it still not work, you can print some log in your CaptureActivity@handleDecode()
to see whether zxing has recognized the code but not pass it to the pre Activity or zxing do not work at all. I will read your code more carefully.
I already have that code in my manifest, also i'm not working with RxJava
.
public void handleDecode(Result rawResult, Bundle bundle) {
inactivityTimer.onActivity();
// Print some log here
Log.d("handleDecode", "result" + rawResult.getText());
}
The log will show whether zxing works well.
It doesn't even enter to the handleDecode
however in the MainActivity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// This print 1
Log.v("MainActivity", String.valueOf(requestCode) );
switch (requestCode) {
case SCANNING_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
if (null != bundle) {
Toast.makeText(this, bundle.getString("result"), Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(this, "Cancel" , Toast.LENGTH_SHORT).show();
}
}
break;
default:
break;
}
}
public final static int SCANNING_REQUEST_CODE = 1;
public final static int REQUEST_CAMERA_PERMISSION_CODE = 0;
public static final String ACTION_SCAN_CODE = "project.selim.com.mo.MainActivity";
Now that the code do not enter handleDecode
, the bug must be in the CaptureActivty
. You could print some log at CaptureActivityHandler
- handleMessage
, see the msg.what value.
@Override
public void handleMessage(Message message) {
Log.d("what", "" + message.what);
}
prints this : D/what: 2131755014
Is it same to the code of express sheet that you scanned with?
I don't know ! where to find the code of express sheet?
Maybe I made a mistake. I mean is the log result right? same to the bar code or QR code that you are scanning with?
Yes the log result are right ! And i'm trying to scan QR code. I just tried to scan a bar code and it works fine.
Hi, @houcem365 .
Have your solved the problem yet? If your answer is positive, then I will close this issue. Or we can discuss the way to solve the problem.
Negative, the problem remain.
Currently i'm not using it, but if you happen to find a solution you may notify me ! Thank you in advance.
Ok, I will try it.
I will close this issue. If you find any solutions to solve it or have any other problems, DO NOT hesitate to reopen it or just open another one. Happy coding 😃!