Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @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;
- @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()
- {
- super.onResume();
- initializeCamera();
- sensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_UI);
- }
- private void initializeCamera()
- {
- mCamera=null;
- Camera.CameraInfo info=new Camera.CameraInfo();
- int cameraCount=Camera.getNumberOfCameras();
- for(int cameraIndex=0;cameraIndex<cameraCount;cameraIndex++)
- {
- try
- {
- Camera.getCameraInfo(cameraIndex, info);
- if(cameraIndex==info.CAMERA_FACING_BACK)
- {
- mCamera=Camera.open(cameraIndex);
- }
- mPreview=new CameraPreview(this,mCamera);
- container.addView(mPreview);
- isPreviewShowing=true;
- }
- catch(Exception ex)
- {
- Log.d(TAG, "Could not open camera");
- }
- }
- }
- 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()
- {
- 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()
- {
- super.onPause();
- sensorManager.unregisterListener(this);
- isPreviewShowing=false;
- killCamera();
- container.removeAllViews();
- }
- @Override
- public void onDestroy()
- {
- super.onDestroy();
- }
- private void killCamera()
- {
- if(mCamera!=null)
- {
- mCamera.cancelAutoFocus();
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement