Android Open Source - GlassCounter Capture Activity






From Project

Back to project page GlassCounter.

License

The source code is released under:

Apache License

If you think the Android project GlassCounter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2008 ZXing authors/*from  w ww .  j a va2s .c om*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.barcodeeye.scan;

import java.io.IOException;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.github.barcodeeye.BaseGlassActivity;
import com.github.barcodeeye.R;
import com.github.barcodeeye.image.ImageManager;
import com.github.barcodeeye.migrated.AmbientLightManager;
import com.github.barcodeeye.migrated.BeepManager;
import com.github.barcodeeye.migrated.FinishListener;
import com.github.barcodeeye.migrated.InactivityTimer;
import com.github.barcodeeye.scan.result.ResultProcessor;
import com.github.barcodeeye.scan.result.ResultProcessorFactory;
import com.github.barcodeeye.scan.ui.ViewfinderView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.Result;
import com.google.zxing.ResultMetadataType;
import com.google.zxing.ResultPoint;
import com.google.zxing.client.android.camera.CameraManager;

public final class CaptureActivity extends BaseGlassActivity implements
SurfaceHolder.Callback {

  private static final String IMAGE_PREFIX = "BarcodeEye_";

  private static final String TAG = CaptureActivity.class.getSimpleName();

  @SuppressWarnings("unused")
  private static final Collection<ResultMetadataType> DISPLAYABLE_METADATA_TYPES = EnumSet
  .of(ResultMetadataType.ISSUE_NUMBER,
      ResultMetadataType.SUGGESTED_PRICE,
      ResultMetadataType.ERROR_CORRECTION_LEVEL,
      ResultMetadataType.POSSIBLE_COUNTRY);

  private CameraManager mCameraManager;
  private CaptureActivityHandler mHandler;
  private Result mSavedResultToShow;
  private ViewfinderView mViewfinderView;
  private boolean mHasSurface;
  private Map<DecodeHintType, ?> mDecodeHints;
  private InactivityTimer mInactivityTimer;
  private BeepManager mBeepManager;
  private AmbientLightManager mAmbientLightManager;
  private ImageManager mImageManager;

  public static Intent newIntent(Context context) {
    Intent intent = new Intent(context, CaptureActivity.class);
    return intent;
  }

  public ViewfinderView getViewfinderView() {
    return mViewfinderView;
  }

  public Handler getHandler() {
    return mHandler;
  }

  CameraManager getCameraManager() {
    return mCameraManager;
  }

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_capture);

    mImageManager = new ImageManager(this);

    mHasSurface = false;
    mInactivityTimer = new InactivityTimer(this);
    mBeepManager = new BeepManager(this);
    mAmbientLightManager = new AmbientLightManager(this);

    mViewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);

    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
  }

  @Override
  protected void onResume() {
    super.onResume();

    mCameraManager = new CameraManager(getApplication());
    mViewfinderView.setCameraManager(mCameraManager);

    mHandler = null;

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    if (mHasSurface) {
      initCamera(surfaceHolder);
    } else {
      surfaceHolder.addCallback(this);
    }

    mBeepManager.updatePrefs();
    mAmbientLightManager.start(mCameraManager);

    mInactivityTimer.onResume();
  }

  @Override
  protected void onPause() {
    if (mHandler != null) {
      mHandler.quitSynchronously();
      mHandler = null;
    }
    mInactivityTimer.onPause();
    mAmbientLightManager.stop();
    mCameraManager.closeDriver();
    if (!mHasSurface) {
      SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
      SurfaceHolder surfaceHolder = surfaceView.getHolder();
      surfaceHolder.removeCallback(this);
    }
    super.onPause();
  }

  @Override
  protected boolean onTap() {
    openOptionsMenu();
    return super.onTap();
  }

  @Override
  protected void onDestroy() {
    mInactivityTimer.shutdown();
    super.onDestroy();
  }

  private void decodeOrStoreSavedBitmap(Bitmap bitmap, Result result) {
    // Bitmap isn't used yet -- will be used soon
    if (mHandler == null) {
      mSavedResultToShow = result;
    } else {
      if (result != null) {
        mSavedResultToShow = result;
      }
      if (mSavedResultToShow != null) {
        Message message = Message.obtain(mHandler,
            R.id.decode_succeeded, mSavedResultToShow);
        mHandler.sendMessage(message);
      }
      mSavedResultToShow = null;
    }
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    if (holder == null) {
      Log.e(TAG,
          "*** WARNING *** surfaceCreated() gave us a null surface!");
    }
    if (!mHasSurface) {
      mHasSurface = true;
      initCamera(holder);
    }
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    mHasSurface = false;
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {

  }

  
  public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
    mInactivityTimer.onActivity();

    boolean fromLiveScan = barcode != null;
    if (fromLiveScan) {
      mBeepManager.playBeepSoundAndVibrate();
      drawResultPoints(barcode, scaleFactor, rawResult, getResources()
          .getColor(R.color.result_points));

      String rawString = rawResult.getText();
      Intent i = new Intent(this, FinalActivity.class);
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      i.putExtra("data", rawString);
      startActivity(i);
      finish();

    }

    
  }

  /**
   * Superimpose a line for 1D or dots for 2D to highlight the key features of
   * the barcode.
   *
   * @param barcode
   *            A bitmap of the captured image.
   * @param scaleFactor
   *            amount by which thumbnail was scaled
   * @param rawResult
   *            The decoded results which contains the points to draw.
   */
  private static void drawResultPoints(Bitmap barcode, float scaleFactor,
      Result rawResult, int color) {
    ResultPoint[] points = rawResult.getResultPoints();
    if (points != null && points.length > 0) {
      Canvas canvas = new Canvas(barcode);
      Paint paint = new Paint();
      paint.setColor(color);
      if (points.length == 2) {
        paint.setStrokeWidth(4.0f);
        drawLine(canvas, paint, points[0], points[1], scaleFactor);
      } else if (points.length == 4
          && (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult
          .getBarcodeFormat() == BarcodeFormat.EAN_13)) {
        // Hacky special case -- draw two lines, for the barcode and metadata
        drawLine(canvas, paint, points[0], points[1], scaleFactor);
        drawLine(canvas, paint, points[2], points[3], scaleFactor);
      } else {
        paint.setStrokeWidth(10.0f);
        for (ResultPoint point : points) {
          if (point != null) {
            canvas.drawPoint(scaleFactor * point.getX(),
                scaleFactor * point.getY(), paint);
          }
        }
      }
    }
  }

  private static void drawLine(Canvas canvas, Paint paint, ResultPoint a,
      ResultPoint b, float scaleFactor) {
    if (a != null && b != null) {
      canvas.drawLine(scaleFactor * a.getX(), scaleFactor * a.getY(),
          scaleFactor * b.getX(), scaleFactor * b.getY(), paint);
    }
  }

  @SuppressWarnings("unused")
  private void handleDecodeInternally(Result rawResult, Bitmap barcode) {

    Uri imageUri = null;
    String imageName = IMAGE_PREFIX + System.currentTimeMillis() + ".png";
    Log.v(TAG, "Saving image as: " + imageName);
    try {
      imageUri = mImageManager.saveImage(imageName, barcode);
    } catch (IOException e) {
      Log.e(TAG, "Failed to save image!", e);
    }

    ResultProcessor<?> processor = ResultProcessorFactory
        .makeResultProcessor(this, rawResult, imageUri);

    startActivity(ResultsActivity.newIntent(this,
        processor.getCardResults()));
  }

  private void initCamera(SurfaceHolder surfaceHolder) {
    if (surfaceHolder == null) {
      throw new IllegalStateException("No SurfaceHolder provided");
    }
    if (mCameraManager.isOpen()) {
      Log.w(TAG,
          "initCamera() while already open -- late SurfaceView callback?");
      return;
    }
    try {
      mCameraManager.openDriver(surfaceHolder);
      if (mHandler == null) {
        mHandler = new CaptureActivityHandler(this, null, mDecodeHints,
            null, mCameraManager);
      }

      decodeOrStoreSavedBitmap(null, null);
    } catch (IOException e) {
      Log.w(TAG, e);
      displayFrameworkBugMessageAndExit();
    } catch (InterruptedException e) {
      Log.w(TAG, e);
      displayFrameworkBugMessageAndExit();
    }
  }

  
  private void displayFrameworkBugMessageAndExit() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.app_name));
    builder.setMessage(getString(R.string.msg_camera_framework_bug));
    builder.setPositiveButton(R.string.button_ok, new FinishListener(this));
    builder.setOnCancelListener(new FinishListener(this));
    builder.show();
  }

  public void restartPreviewAfterDelay(long delayMS) {
    if (mHandler != null) {
      mHandler.sendEmptyMessageDelayed(R.id.restart_preview, delayMS);
    }
  }

  public void drawViewfinder() {
    mViewfinderView.drawViewfinder();
  }
}




Java Source Code List

com.github.barcodeeye.BaseGlassActivity.java
com.github.barcodeeye.LaunchActivity.java
com.github.barcodeeye.image.ImageManager.java
com.github.barcodeeye.migrated.AmbientLightManager.java
com.github.barcodeeye.migrated.BeepManager.java
com.github.barcodeeye.migrated.DecodeFormatManager.java
com.github.barcodeeye.migrated.DecodeHintManager.java
com.github.barcodeeye.migrated.FinishListener.java
com.github.barcodeeye.migrated.HttpHelper.java
com.github.barcodeeye.migrated.InactivityTimer.java
com.github.barcodeeye.migrated.Intents.java
com.github.barcodeeye.migrated.LocaleManager.java
com.github.barcodeeye.scan.CaptureActivityHandler.java
com.github.barcodeeye.scan.CaptureActivity.java
com.github.barcodeeye.scan.DecodeHandler.java
com.github.barcodeeye.scan.DecodeThread.java
com.github.barcodeeye.scan.FinalActivity.java
com.github.barcodeeye.scan.FourthActivity.java
com.github.barcodeeye.scan.ResultsActivity.java
com.github.barcodeeye.scan.api.CardPresenter.java
com.github.barcodeeye.scan.result.ResultProcessorFactory.java
com.github.barcodeeye.scan.result.ResultProcessor.java
com.github.barcodeeye.scan.result.internal.IsbnResultProcessor.java
com.github.barcodeeye.scan.result.internal.ProductResultProcessor.java
com.github.barcodeeye.scan.result.internal.TextResultProcessor.java
com.github.barcodeeye.scan.result.internal.UriResultProcessor.java
com.github.barcodeeye.scan.result.supplement.AmazonInfoRetriever.java
com.github.barcodeeye.scan.result.supplement.BookResultInfoRetriever.java
com.github.barcodeeye.scan.result.supplement.ProductResultInfoRetriever.java
com.github.barcodeeye.scan.result.supplement.SupplementalInfoRetriever.java
com.github.barcodeeye.scan.result.supplement.TitleRetriever.java
com.github.barcodeeye.scan.result.supplement.URIResultInfoRetriever.java
com.github.barcodeeye.scan.ui.ViewfinderView.java
com.google.zxing.client.android.camera.AutoFocusManager.java
com.google.zxing.client.android.camera.CameraConfigurationManager.java
com.google.zxing.client.android.camera.CameraManager.java
com.google.zxing.client.android.camera.PreviewCallback.java
com.google.zxing.client.android.camera.open.OpenCameraInterface.java
com.google.zxing.client.android.clipboard.ClipboardInterface.java
com.google.zxing.client.android.encode.ContactEncoder.java
com.google.zxing.client.android.encode.Formatter.java
com.google.zxing.client.android.encode.MECARDContactEncoder.java
com.google.zxing.client.android.encode.VCardContactEncoder.java
com.google.zxing.client.android.history.DBHelper.java
com.google.zxing.client.android.history.HistoryItemAdapter.java
com.google.zxing.client.android.history.HistoryItem.java
com.google.zxing.client.android.share.AppInfo.java
com.google.zxing.client.android.share.AppPickerActivity.java
com.google.zxing.client.android.share.BookmarkAdapter.java
com.google.zxing.client.android.share.BookmarkPickerActivity.java
com.google.zxing.client.android.share.LoadPackagesAsyncTask.java
com.google.zxing.client.android.wifi.NetworkType.java
com.google.zxing.client.android.wifi.WifiConfigManager.java