我试图从图片中读取条形码,但我只能读取二维码?我可以从下面的代码中看到,尝试使位图更高,因为我希望图像分辨率使图像不可读。但这没有任何帮助。
条形码活动:
package lassebjoerklund.easyfridge;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.util.SparseArray;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.File;
import java.net.URI;
public class BarcodeTracking extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Bitmap image;
private BarcodeDetector detector;
private ImageView scanedImage;
private Uri mImageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode_tracking);
scanedImage = (ImageView) findViewById(R.id.ivScanedImage);
detector = new BarcodeDetector.Builder(getApplicationContext()).build();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File("");
try {
photo = createTemporaryFile("picture", ".jpg");
photo.delete();
} catch (Exception e) {
e.printStackTrace();
Log.v(getApplication().getClass().getSimpleName(), "Can't create file to take picture!");
Toast.makeText(getApplicationContext(), "Please check SD card! Image shot is impossible!", Toast.LENGTH_SHORT);
}
mImageUri = Uri.fromFile(photo);
if (intent.resolveActivity(getPackageManager()) != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
grabImage(scanedImage);
if(image != null) {
this.grabImage(scanedImage);
if (!detector.isOperational()) {
Log.d("Detector Setup", "could not setup detector");
} else {
Frame frame = new Frame.Builder().setBitmap(image).build();
SparseArray<Barcode> barcodes = detector.detect(frame);
if (barcodes.size() != 0) {
Barcode resultedBarcode = barcodes.valueAt(0);
Log.d("Barcode", "" + resultedBarcode.rawValue);
}
}
}
}
}
@Override
protected void onResume() {
super.onResume();
}
private File createTemporaryFile(String part, String ext) throws Exception {
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
if (!tempDir.exists()) {
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}
private void grabImage(ImageView imageView) {
this.getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
image = bitmap;
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.d(getApplication().getClass().getSimpleName(), "Failed to load", e);
}
}
}
我似乎无法找到我做错的事。有人可以帮忙吗?
更新
发现它在Gs6边缘工作,但在我的nexus 5上没有...两者都在运行6.0.1 android
更新2
现在让它工作了。问题是距图像的距离。我没有把手机放在离代码5厘米处,而是试了一会儿。
但是我仍会将问题保持开放,以防万一在代码中发现错误