package com.fanxin.huangfangyi.main.activity; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Rect; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.hardware.Camera.PreviewCallback; import android.hardware.Camera.Size; import android.os.Bundle; import android.os.Handler; import android.text.TextUtils; import android.view.Display; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.fanxin.huangfangyi.R; import com.fanxin.huangfangyi.main.FXConstant; import com.fanxin.huangfangyi.main.widget.scan.CameraManager; import com.fanxin.huangfangyi.main.widget.scan.CameraPreview; 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 java.io.IOException; import java.lang.reflect.Field; public class ScanCaptureActivity extends Activity implements OnClickListener{ private Camera mCamera; private CameraPreview mPreview; private Handler autoFocusHandler; private CameraManager mCameraManager; private TextView scanResult; private FrameLayout scanPreview; private Button scanRestart; private RelativeLayout scanContainer; private RelativeLayout scanCropView; private ImageView scanLine; private Rect mCropRect = null; private boolean barcodeScanned = false; private boolean previewing = true; private ImageScanner mImageScanner = null; private ImageView iv_back,iv_camera; private TextView tv_title; private RelativeLayout titleBar; private String cursor; static { System.loadLibrary("iconv"); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setContentView(R.layout.widget_zbar_scan_capture); findViewById(); iniData(); addEvents(); initViews(); } private void iniData() { //标题上面的操作 iv_camera.setVisibility(View.GONE); tv_title.setText("二维码/条码 扫描"); } private void findViewById() { scanPreview = (FrameLayout) findViewById(R.id.capture_preview); scanResult = (TextView) findViewById(R.id.capture_scan_result); scanRestart = (Button) findViewById(R.id.capture_restart_scan); scanContainer = (RelativeLayout) findViewById(R.id.capture_container); scanCropView = (RelativeLayout) findViewById(R.id.capture_crop_view); scanLine = (ImageView) findViewById(R.id.capture_scan_line); iv_back = (ImageView) findViewById(R.id.iv_back); iv_camera = (ImageView) findViewById(R.id.iv_camera); tv_title = (TextView) findViewById(R.id.tv_title); titleBar = (RelativeLayout) findViewById(R.id.title); } private void addEvents() { iv_back.setOnClickListener(this); scanRestart.setOnClickListener(this); } private void initViews() { mImageScanner = new ImageScanner(); mImageScanner.setConfig(0, Config.X_DENSITY, 3); mImageScanner.setConfig(0, Config.Y_DENSITY, 3); autoFocusHandler = new Handler(); mCameraManager = new CameraManager(this); try { mCameraManager.openDriver(); } catch (IOException e) { e.printStackTrace(); } // 调整扫描框大小,自适应屏幕 Display display = this.getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) scanCropView .getLayoutParams(); linearParams.height = (int) (width * 0.8); linearParams.width = (int) (width * 0.8); scanCropView.setLayoutParams(linearParams); // ** mCamera = mCameraManager.getCamera(); mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB); scanPreview.addView(mPreview); 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.85f); animation.setDuration(5000); animation.setRepeatCount(-1); animation.setRepeatMode(Animation.REVERSE); scanLine.startAnimation(animation); } public void onPause() { super.onPause(); releaseCamera(); } private void releaseCamera() { if (mCamera != null) { previewing = false; mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } } private Runnable doAutoFocus = new Runnable() { public void run() { if (previewing) mCamera.autoFocus(autoFocusCB); } }; PreviewCallback previewCb = new PreviewCallback() { public void onPreviewFrame(byte[] data, Camera camera) { Size size = camera.getParameters().getPreviewSize(); // 这里需要将获取的data翻转一下,因为相机默认拿的的横屏的数据 byte[] rotatedData = new byte[data.length]; for (int y = 0; y < size.height; y++) { for (int x = 0; x < size.width; x++) rotatedData[x * size.height + size.height - y - 1] = data[x + y * size.width]; } // 宽高也要调整 int tmp = size.width; size.width = size.height; size.height = tmp; initCrop(); Image barcode = new Image(size.width, size.height, "Y800"); barcode.setData(rotatedData); barcode.setCrop(mCropRect.left, mCropRect.top, mCropRect.width(), mCropRect.height()); int result = mImageScanner.scanImage(barcode); String resultStr = null; if (result != 0) { SymbolSet syms = mImageScanner.getResults(); for (Symbol sym : syms) { resultStr = sym.getData(); } } if (!TextUtils.isEmpty(resultStr)) { previewing = false; mCamera.setPreviewCallback(null); mCamera.stopPreview(); releaseCamera(); barcodeScanned = true; if (resultStr.contains(":")) { String type = resultStr .substring(0, resultStr.indexOf(":")); String value = resultStr .substring(resultStr.indexOf(":") + 1); System.out.println("type----------->>>>" + type); System.out.println("value----------->>>>" + value); if (type.equals("userInfo")) { startActivity(new Intent(ScanCaptureActivity.this, UserDetailsActivity.class).putExtra( FXConstant.KEY_USER_INFO, value)); }else { Toast.makeText(getApplicationContext(), "无效二维码", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "无效二维码", Toast.LENGTH_SHORT).show(); } // // Intent rIntent = new Intent(); // rIntent.putExtra("SCAN_RESULT",resultStr); // setResult(RESULT_OK,rIntent); finish(); // scanResult.setText("barcode result " + resultStr); // barcodeScanned = true; } } }; // Mimic continuous auto-focusing AutoFocusCallback autoFocusCB = new AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { autoFocusHandler.postDelayed(doAutoFocus, 1000); } }; /** * 初始化截取的矩形区域 */ private void initCrop() { int cameraWidth = mCameraManager.getCameraResolution().y; int cameraHeight = mCameraManager.getCameraResolution().x; /** 获取布局中扫描框的位置信息 */ 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(); /** 获取布局容器的宽高 */ int containerWidth = scanContainer.getWidth(); int containerHeight = scanContainer.getHeight(); /** 计算最终截取的矩形的左上角顶点x坐标 */ int x = cropLeft * cameraWidth / containerWidth; /** 计算最终截取的矩形的左上角顶点y坐标 */ int y = cropTop * cameraHeight / containerHeight; /** 计算最终截取的矩形的宽度 */ int width = cropWidth * cameraWidth / containerWidth; /** 计算最终截取的矩形的高度 */ int height = cropHeight * cameraHeight / containerHeight; /** 生成最终的截取的矩形 */ 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; } @Override public void onClick(View v) { switch(v.getId()){ case R.id.iv_back: releaseCamera(); finish(); break; case R.id.capture_restart_scan: if (barcodeScanned) { barcodeScanned = false; scanResult.setText("Scanning..."); mCamera.setPreviewCallback(previewCb); mCamera.startPreview(); previewing = true; mCamera.autoFocus(autoFocusCB); } break; } } }