Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.blutechnologies.scancard;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Matrix;
- import android.hardware.Camera;
- import android.hardware.Camera.AutoFocusCallback;
- import android.hardware.Camera.CameraInfo;
- import android.hardware.Camera.Parameters;
- import android.hardware.Camera.PictureCallback;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.media.ExifInterface;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Debug;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v4.app.FragmentActivity;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.OrientationEventListener;
- import android.view.Surface;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.FrameLayout;
- @TargetApi(Build.VERSION_CODES.GINGERBREAD)
- public class CameraActivity extends FragmentActivity implements SensorEventListener,ShutterButton.ShutterButtonListener,View.OnClickListener,KeyEvent.Callback
- {
- //hope new Camera API works
- long sFocusTime;
- long sClickTime;
- long sSensorCallTime;
- static final int FOCUS_NOT_STARTED=1;
- static final int FOCUSING=2;
- static final int FOCUSING_SNAP_ON_FINISH=3;
- static final int FOCUS_SUCCESS=4;
- static final int FOCUS_FAIL=5;
- private int mFocusState=FOCUS_NOT_STARTED;
- static final int CAMERA_IDLE=1;
- static final int SNAPSHOT_IN_PROGRESS=2;
- private int mCameraStatus;
- private boolean isPreviewShowing=false;
- static final String TAG="CameraActivity";
- SensorManager sensorManager;
- Sensor sensor;
- FrameLayout container;
- ShutterButton btn_capture;
- boolean isClicked;
- CameraPreview mPreview;
- Camera mCamera;
- boolean notContinuousFocus=false;
- boolean blurred;
- float mLastX,mLastY,mLastZ;
- String pathName;
- private static final int FIRST_TIME_INIT = 1;
- private static final int CLEAR_SCREEN_DELAY = 2;
- private MyOrientationEventListener mOrientationListener;
- // The device orientation in degrees. Default is unknown.
- private int mOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
- // The orientation compensation for icons and thumbnails.
- private int mOrientationCompensation = 0;
- private boolean mPreviewing;
- private boolean mPausing;
- private boolean mFirstTimeInitialized;
- @Override
- public void onAccuracyChanged(Sensor event, int accuracy) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onSensorChanged(SensorEvent event) {
- // TODO Auto-generated method stub
- if(sFocusTime!=0 &&sClickTime==0)
- {
- sSensorCallTime=System.currentTimeMillis();
- Log.d(TAG,"Sensor call time"+sSensorCallTime);
- }
- if(!isClicked){
- float x=event.values[0];
- float y=event.values[1];
- float z=event.values[2];
- if(mLastX==0)
- mLastX=x;
- if(mLastY==0)
- mLastY=y;
- if(mLastZ==0)
- mLastZ=z;
- float deltaX=x-mLastX;
- float deltaY=y-mLastY;
- float deltaZ=z-mLastZ;
- if(deltaX>2.5 || deltaY>2.5 || deltaZ>2.5)
- {
- if(mFocusState!=FOCUSING && mFocusState!=FOCUSING_SNAP_ON_FINISH)
- {
- mFocusState=FOCUS_NOT_STARTED;
- Log.d(TAG, "Focus called from accelerometer");
- doFocus(true);
- }
- }
- mLastX=x;
- mLastY=y;
- mLastZ=z;
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_camera);
- sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
- sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- container=(FrameLayout)findViewById(R.id.container);
- btn_capture=(ShutterButton)findViewById(R.id.btn_capture);
- btn_capture.setShutterButtonListener(this);
- btn_capture.setVisibility(View.VISIBLE);
- btn_capture.setClickable(true);
- }
- @Override
- public void onResume()
- {
- if(mFirstTimeInitialized)
- initializeSecondTime();
- else
- initializeFirstTime();
- super.onResume();
- initializeCamera();
- sensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_UI);
- }
- private void initializeCamera()
- {
- try
- {
- mCamera=Camera.open();
- }
- catch(Exception ex)
- {
- Log.d(TAG, "Could not open camera");
- }
- mPreview=new CameraPreview(this,mCamera);
- container.addView(mPreview);
- isPreviewShowing=true;
- }
- private void autoFocus()
- {
- //Confused about the condition:
- if(mFocusState==FOCUS_NOT_STARTED && isPreviewShowing && mCameraStatus!=SNAPSHOT_IN_PROGRESS)
- {
- mFocusState=FOCUSING;
- mCamera.autoFocus(autoFocusCallback);
- }
- }
- private void cancelAutoFocus()
- {
- //Confused about the condition
- if(mCameraStatus==CAMERA_IDLE && (mFocusState==FOCUSING||mFocusState==FOCUS_SUCCESS||mFocusState==FOCUS_FAIL))
- {
- mCamera.cancelAutoFocus();
- }
- mFocusState=FOCUS_NOT_STARTED;
- }
- private void doSnap()
- {
- int rotation=0;
- if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
- CameraInfo info = new CameraInfo();
- Camera.getCameraInfo(CameraInfo.CAMERA_FACING_BACK, info);
- if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
- rotation = (info.orientation - mOrientation + 360) % 360;
- } else { // back-facing camera
- rotation = (info.orientation + mOrientation) % 360;
- }
- }
- Camera.Parameters params=mCamera.getParameters();
- params.setRotation(rotation);
- mCamera.setParameters(params);
- String mFocusMode=mCamera.getParameters().getFocusMode();
- if(mFocusMode.equals(Parameters.FOCUS_MODE_INFINITY)||mFocusMode.equals(Parameters.FOCUS_MODE_EDOF)||mFocusMode.equals(mFocusMode.equals(Parameters.FOCUS_MODE_FIXED))||mFocusState==FOCUS_SUCCESS||mFocusState==FOCUS_FAIL||mFocusState==FOCUS_NOT_STARTED)
- {
- //take a picture here
- //If the focus was not started,it will go through the focus states if the user holds on to the ShutterButton until the Camera focuses...
- //If it does come to do snap
- mCameraStatus=SNAPSHOT_IN_PROGRESS;
- mCamera.takePicture(null, null, pictureCallback);
- }
- else if(mFocusState==FOCUSING)
- mFocusState=FOCUSING_SNAP_ON_FINISH;
- }
- private void doFocus(boolean pressed)
- {
- //Pressed is set to true,if ShutterButton is half-pressed
- if(pressed)
- autoFocus();
- else
- cancelAutoFocus();
- }
- @Override
- public void onPause()
- {
- mOrientationListener.disable();
- sensorManager.unregisterListener(this);
- isPreviewShowing=false;
- if(mCamera!=null)
- killCamera();
- container.removeAllViews();
- super.onPause();
- }
- private void killCamera()
- {
- if(mCamera!=null)
- {
- mCamera.cancelAutoFocus();
- mCamera.setPreviewCallback(null);
- mCamera.stopPreview();
- mCamera.release();
- mCamera=null;
- }
- }
- AutoFocusCallback autoFocusCallback=new AutoFocusCallback(){
- @Override
- public void onAutoFocus(boolean success, Camera camera) {
- // TODO Auto-generated method stub
- Log.d(TAG, "Started focusing");
- if(mFocusState==FOCUSING_SNAP_ON_FINISH)
- {
- //Take a picture regardless of whether the focus succeeds or fails:
- if(success)
- mFocusState=FOCUS_SUCCESS;
- else
- mFocusState=FOCUS_FAIL;
- doSnap();
- }
- else if(mFocusState==FOCUSING)
- {
- if(success)
- mFocusState=FOCUS_SUCCESS;
- else
- mFocusState=FOCUS_FAIL;
- }
- else if(mFocusState==FOCUS_NOT_STARTED)
- {
- Log.d(TAG,"Focus not started,doing nothing");
- }
- }
- };
- PictureCallback pictureCallback=new PictureCallback()
- {
- @Override
- public void onPictureTaken(final byte[] data, Camera camera) {
- // TODO Auto-generated method stub
- Log.d(TAG,"In picture callback");
- mCameraStatus=CAMERA_IDLE;
- String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
- File dir=new File(path);
- if(!dir.exists())
- dir.mkdirs();
- String fileName="IMG_"+System.currentTimeMillis()+".jpg";
- pathName=path+File.separator+fileName;
- new Thread()
- {
- public void run()
- {
- try {
- FileOutputStream fos=new FileOutputStream(pathName);
- fos.write(data);
- Log.d(TAG, "Data saved to file successfully");
- } catch (FileNotFoundException e) {
- Log.e(TAG, "The output file could not be found",e);
- } catch (IOException e) {
- Log.e(TAG, "There was an exception writing from byte[] to file",e);
- }
- try {
- ExifInterface exif=new ExifInterface(pathName.toString());
- int exifOrientation=exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
- Log.d(TAG,"Orient:"+exifOrientation);
- int rotate=0;
- switch(exifOrientation)
- {
- case ExifInterface.ORIENTATION_ROTATE_90:
- rotate=90;
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- rotate=180;
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- rotate=270;
- break;
- }
- Log.d(TAG,"Rotation:"+rotate);
- if(rotate!=0)
- {
- Bitmap bitmap=BitmapFactory.decodeFile(pathName);
- int w=bitmap.getWidth();
- int h=bitmap.getHeight();
- Matrix mtx=new Matrix();
- mtx.preRotate(rotate);
- bitmap=Bitmap.createBitmap(bitmap, 0, 0,w,h,mtx,false);
- bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
- FileOutputStream fos;
- try {
- fos = new FileOutputStream(pathName);
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
- } catch (FileNotFoundException e) {
- Log.e(TAG, "Error saving rotated bitmap to file,e");
- }
- }
- }
- catch(IOException e) {
- Log.e(TAG,"Rotate or coversion failed:"+e.toString());
- }
- }
- }.start();
- Intent intent=new Intent(getBaseContext(),ImagePreviewActivity.class);
- intent.putExtra("pathName", pathName);
- isPreviewShowing=false;
- startActivity(intent);
- }
- };
- @Override
- public void onShutterButtonFocus(ShutterButton shutterButton,
- boolean pressed) {
- // TODO Auto-generated method stub
- sFocusTime=System.currentTimeMillis();
- Log.d(TAG, "Shutter button focus called "+sFocusTime);
- switch(shutterButton.getId())
- {
- case R.id.btn_capture:
- {
- isClicked=pressed;
- doFocus(pressed);
- break;
- }
- }
- }
- @Override
- public void onShutterButtonClick(ShutterButton shutterButton) {
- // TODO Auto-generated method stub
- Log.d(TAG, "Shutter button click called");
- sClickTime=System.currentTimeMillis();
- Log.d(TAG,"ShutterButtonClick time: "+sClickTime);
- Log.d(TAG,"Time taken to go to click"+((float)(sClickTime-sFocusTime)/1000));
- switch(shutterButton.getId())
- {
- case R.id.btn_capture:
- doSnap();
- isClicked=false;
- break;
- }
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //This assumes that the clicks the screen only if the focus has not been set...
- //This feature is useful in phones where accelerometer delay is unacceptable
- doFocus(true);
- }
- @Override
- public boolean onKeyDown(int keyCode,KeyEvent event)
- {
- switch(keyCode)
- {
- case KeyEvent.KEYCODE_BACK:
- startActivity(new Intent(this,MainActivity.class));
- }
- return false;
- }
- private class MainHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case CLEAR_SCREEN_DELAY: {
- getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- break;
- }
- case FIRST_TIME_INIT: {
- initializeFirstTime();
- break;
- }
- }
- }
- }
- private void initializeFirstTime()
- {
- mOrientationListener = new MyOrientationEventListener(CameraActivity.this);
- mOrientationListener.enable();
- mFirstTimeInitialized = true;
- }
- private void initializeSecondTime()
- {
- mOrientationListener.enable();
- }
- private class MyOrientationEventListener extends OrientationEventListener {
- public MyOrientationEventListener(Context context) {
- super(context);
- }
- @Override
- public void onOrientationChanged(int orientation) {
- // We keep the last known orientation. So if the user first orient
- // the camera then point the camera to floor or sky, we still have
- // the correct orientation.
- if (orientation == ORIENTATION_UNKNOWN) return;
- mOrientation = roundOrientation(orientation);
- // When the screen is unlocked, display rotation may change. Always
- // calculate the up-to-date orientationCompensation.
- int orientationCompensation = mOrientation+ getDisplayRotation(CameraActivity.this);
- if (mOrientationCompensation != orientationCompensation) {
- mOrientationCompensation = orientationCompensation;
- }
- }
- public int roundOrientation(int orientation) {
- return ((orientation + 45) / 90 * 90) % 360;
- }
- public int getDisplayRotation(Activity activity) {
- int rotation = activity.getWindowManager().getDefaultDisplay()
- .getRotation();
- switch (rotation) {
- case Surface.ROTATION_0: return 0;
- case Surface.ROTATION_90: return 90;
- case Surface.ROTATION_180: return 180;
- case Surface.ROTATION_270: return 270;
- }
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment