9
0
mirror of https://gitee.com/shuto/customCamera.git synced 2026-05-02 00:07:24 +08:00

Ajout des fonctions de gestion du bouton miniature et de la gestion de la position de ce bouton.

This commit is contained in:
Thomas BOY
2014-12-23 10:30:57 +01:00
parent e458ac7789
commit 4774fd4ca0
2 changed files with 437 additions and 339 deletions
@@ -20,8 +20,6 @@
android:id="@+id/background" android:id="@+id/background"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:alpha="0.2" android:alpha="0.2"
android:scaleType="fitXY" /> android:scaleType="fitXY" />
@@ -45,7 +43,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0.2" android:layout_weight="0.2"
android:onClick="showMiniature" android:onClick="buttonMiniature"
android:text="@string/miniature" /> android:text="@string/miniature" />
<Button <Button
@@ -94,6 +92,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:onClick="acceptPhoto"
android:text="@string/acceptePicture" /> android:text="@string/acceptePicture" />
<Button <Button
@@ -101,6 +100,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:onClick="declinePhoto"
android:text="@string/declinePicture" /> android:text="@string/declinePicture" />
</LinearLayout> </LinearLayout>
@@ -2,7 +2,6 @@ package org.geneanet.customcamera;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.CompressFormat;
@@ -41,25 +40,19 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
/** /** Activity to use customCamera. */
* Activity to use customCamera.
*/
public class CameraActivity extends Activity { public class CameraActivity extends Activity {
// Camera resource. // Camera resource.
private Camera customCamera = null; private Camera customCamera = null;
// Distance between fingers for the zoom. // Distance between fingers for the zoom.
private static float distanceBetweenFingers; private static float distanceBetweenFingers;
// Enable miniature mode. // Enable miniature mode.
private boolean modeMiniature = false; private boolean modeMiniature = false;
// Enable when a photo is taken.
private boolean photoTaken = false;
// The image in Bitmap format of the preview photo. // The image in Bitmap format of the preview photo.
private Bitmap storedBitmap; private Bitmap photoTaken = null;
/** Method onCreate. Handle the opacity seekBar and general configuration. */
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -71,7 +64,6 @@ public class CameraActivity extends Activity {
WindowManager.LayoutParams.FLAG_FULLSCREEN); WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera_view); setContentView(R.layout.activity_camera_view);
setBackground(); setBackground();
// The opacity bar // The opacity bar
@@ -82,24 +74,23 @@ public class CameraActivity extends Activity {
int progress = 0; int progress = 0;
@Override @Override
public void onProgressChanged(SeekBar seekBar, int progresValue, public void onProgressChanged(SeekBar seekBar, int progressValue,
boolean fromUser) { boolean fromUser) {
progress = progresValue; progress = progressValue;
ImageView background = (ImageView) findViewById(R.id.background); ImageView background = (ImageView) findViewById(R.id.background);
float newOpacity = (float) (0.2 + progress * 0.1); float newOpacity = (float) (0.2 + progress * 0.1);
background.setAlpha(newOpacity); background.setAlpha(newOpacity);
} }
@Override @Override
public void onStartTrackingTouch(SeekBar seekBar) { public void onStartTrackingTouch(SeekBar seekBar) {}
}
@Override @Override
public void onStopTrackingTouch(SeekBar seekBar) { public void onStopTrackingTouch(SeekBar seekBar) {}
}
}); });
} }
/** Method onStart. Handle the zoom level seekBar and the camera orientation. */
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
@@ -114,25 +105,24 @@ public class CameraActivity extends Activity {
// Get the default orientation of the device // Get the default orientation of the device
int defaultOrientation = getDeviceDefaultOrientation(); int defaultOrientation = getDeviceDefaultOrientation();
// Change camera orientation function of the device's default // Change camera orientation function of the device's default orientation.
// orientation.
if (defaultOrientation == 1 || defaultOrientation == 2) { if (defaultOrientation == 1 || defaultOrientation == 2) {
int orientation; int orientation;
switch (defaultDisplay.getRotation()) { switch (defaultDisplay.getRotation()) {
case 0: case 0:
orientation = (defaultOrientation == 1) ? 90 : 0; orientation = defaultOrientation == Configuration.ORIENTATION_PORTRAIT ? 90 : 0;
customCamera.setDisplayOrientation(orientation); customCamera.setDisplayOrientation(orientation);
break; break;
case 1: case 1:
orientation = (defaultOrientation == 1) ? 0 : 270; orientation = defaultOrientation == Configuration.ORIENTATION_PORTRAIT ? 0 : 270;
customCamera.setDisplayOrientation(orientation); customCamera.setDisplayOrientation(orientation);
break; break;
case 2: case 2:
orientation = (defaultOrientation == 1) ? 270 : 180; orientation = defaultOrientation == Configuration.ORIENTATION_PORTRAIT ? 270 : 180;
customCamera.setDisplayOrientation(orientation); customCamera.setDisplayOrientation(orientation);
break; break;
case 3: case 3:
orientation = (defaultOrientation == 1) ? 180 : 90; orientation = defaultOrientation == Configuration.ORIENTATION_PORTRAIT ? 180 : 90;
customCamera.setDisplayOrientation(orientation); customCamera.setDisplayOrientation(orientation);
break; break;
default: default:
@@ -187,10 +177,53 @@ public class CameraActivity extends Activity {
preview.removeAllViews(); preview.removeAllViews();
} }
/** To set background in the view. */
protected void setBackground() {
// Get the base64 picture for the background only if it's exist.
byte[] imgBackgroundBase64 = TransferBigData.getImgBackgroundBase64();
if (imgBackgroundBase64 != null) {
// Get picture.
Bitmap imgBackgroundBitmap = BitmapFactory.decodeByteArray(
imgBackgroundBase64, 0, imgBackgroundBase64.length);
// Get sizes screen.
Display defaultDisplay = getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
defaultDisplay.getMetrics(displayMetrics);
int displayWidthPx = (int) displayMetrics.widthPixels;
int displayHeightPx = (int) displayMetrics.heightPixels;
// Get sizes picture.
int widthBackground = (int) (imgBackgroundBitmap.getWidth() * displayMetrics.density);
int heightBackground = (int) (imgBackgroundBitmap.getHeight() * displayMetrics.density);
// Change size ImageView.
RelativeLayout.LayoutParams paramsMiniature = new RelativeLayout.LayoutParams(
widthBackground, heightBackground);
float ratioX = (float) displayWidthPx / (float) widthBackground;
float ratioY = (float) displayHeightPx / (float) heightBackground;
if (ratioX < ratioY && ratioX < 1) {
paramsMiniature.width = (int) displayWidthPx;
paramsMiniature.height = (int) (ratioX * heightBackground);
} else if (ratioX >= ratioY && ratioY < 1) {
paramsMiniature.width = (int) (ratioY * widthBackground);
paramsMiniature.height = (int) displayHeightPx;
}
// set image at the view.
ImageView background = (ImageView) findViewById(R.id.background);
background.setImageBitmap(imgBackgroundBitmap);
paramsMiniature.addRule(RelativeLayout.CENTER_IN_PARENT,
RelativeLayout.TRUE);
background.setLayoutParams(paramsMiniature);
}
}
/** Event on touch screen to call the manager of the zoom & the auto focus. */ /** Event on touch screen to call the manager of the zoom & the auto focus. */
@Override @Override
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
if (!photoTaken) { if (photoTaken == null) {
Camera.Parameters paramsCamera = customCamera.getParameters(); Camera.Parameters paramsCamera = customCamera.getParameters();
int action = event.getAction(); int action = event.getAction();
@@ -215,7 +248,6 @@ public class CameraActivity extends Activity {
/** /**
* Determine the space between the first two fingers. * Determine the space between the first two fingers.
*
* @param MotionEvent event Current event which start this calculation. * @param MotionEvent event Current event which start this calculation.
* *
* @return float * @return float
@@ -261,7 +293,6 @@ public class CameraActivity extends Activity {
/** /**
* To set the seekBar zoom with the pinchZoom. * To set the seekBar zoom with the pinchZoom.
*
* @param int maxZoom The max zoom of the device. * @param int maxZoom The max zoom of the device.
* @param int zoom The current zoom. * @param int zoom The current zoom.
*/ */
@@ -274,12 +305,11 @@ public class CameraActivity extends Activity {
/** /**
* Manage the focus. * Manage the focus.
*
* @param event Current event which start this action. * @param event Current event which start this action.
* @param paramsCamera Camera's parameter. * @param paramsCamera Camera's parameter.
*/ */
public void handleFocus(MotionEvent event, Camera.Parameters paramsCamera) { public void handleFocus(MotionEvent event, Camera.Parameters paramsCamera) {
if (photoTaken == false) { if (photoTaken == null) {
List<String> supportedFocusModes = paramsCamera.getSupportedFocusModes(); List<String> supportedFocusModes = paramsCamera.getSupportedFocusModes();
if (supportedFocusModes != null if (supportedFocusModes != null
&& supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
@@ -292,59 +322,69 @@ public class CameraActivity extends Activity {
} }
} }
/** /** Resize and mask the miniature button. */
* Display the miniature. public void buttonMiniature(View view) {
*
* @param view Current view.
*/
public void showMiniature(View view) {
ImageView background = (ImageView) findViewById(R.id.background); ImageView background = (ImageView) findViewById(R.id.background);
final Button miniature = (Button) view; final Button miniature = (Button) view;
// if it's not miniature mode. modeMiniature = true;
if (!modeMiniature) { // Set new size for miniature layout.
modeMiniature = true; setParamsMiniature(background, true);
// Set new size for miniature layout. // Hide the miniature button.
setParamsMiniature(background, true); miniature.setVisibility(View.INVISIBLE);
// Hide the miniature button. // Add event on click action for the miniature picture.
miniature.setVisibility(View.INVISIBLE); background.setOnClickListener(new View.OnClickListener() {
// Add event on click action for the miniature picture. public void onClick(View view) {
background.setOnClickListener(new View.OnClickListener() { modeMiniature = false;
public void onClick(View view) { ImageView background = (ImageView) view;
modeMiniature = false; // Resize miniature.
ImageView background = (ImageView) findViewById(R.id.background); background.setClickable(false);
// resize miniature. setBackground();
background.setClickable(false);
setBackground();
miniature.setVisibility(View.VISIBLE); miniature.setVisibility(View.VISIBLE);
} }
}); });
}
} }
/** /**
* Set the size and the gravity of the miniature function of photo is taken or not. * Set the size and the gravity of the miniature function of photo is taken or not.
*
* @param imageView Reference to the background image. * @param imageView Reference to the background image.
* @param resize Should we resize or not ? Only when click on "miniature". * @param resize Should we resize or not ? Only when click on "miniature".
*/ */
public void setParamsMiniature(ImageView imageView, boolean resize) { public void setParamsMiniature(ImageView imageView, boolean resize) {
RelativeLayout.LayoutParams paramsMiniature = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams paramsMiniature =
imageView.getWidth(), imageView.getHeight()); (RelativeLayout.LayoutParams) imageView.getLayoutParams();
if (resize == true) { if (resize == true) {
paramsMiniature.width = imageView.getWidth() / 4; paramsMiniature.width = paramsMiniature.width / 4;
paramsMiniature.height = imageView.getHeight() / 4; paramsMiniature.height = paramsMiniature.height / 4;
} }
if (!photoTaken) {
// Call the method to handle the position of the miniature.
positioningMiniature(paramsMiniature);
// paramsMiniature.addRule(RelativeLayout.ALIGN_TOP, 0);
imageView.setLayoutParams(paramsMiniature);
}
/**
* Handle the position of the miniature button
* @param paramsMiniature The parameters of the layout.
*/
public void positioningMiniature(RelativeLayout.LayoutParams paramsMiniature) {
if (photoTaken == null) {
paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE); RelativeLayout.TRUE);
} else { } else {
paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_TOP, paramsMiniature.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE); RelativeLayout.TRUE);
} }
imageView.setLayoutParams(paramsMiniature);
} }
/** /**
@@ -374,7 +414,6 @@ public class CameraActivity extends Activity {
/** /**
* Method to take picture. * Method to take picture.
*
* @param view Current view. * @param view Current view.
*/ */
public void takePhoto(View view) { public void takePhoto(View view) {
@@ -396,39 +435,10 @@ public class CameraActivity extends Activity {
/** /**
* Event when picture is taken. * Event when picture is taken.
*
* @param byte[] data Picture with byte format. * @param byte[] data Picture with byte format.
* @param Camera camera Current resource camera. * @param Camera camera Current resource camera.
*/ */
public void onPictureTaken(final byte[] data, Camera camera) { public void onPictureTaken(final byte[] data, Camera camera) {
// Show buttons to accept or decline the picture.
LinearLayout keepPhoto = (LinearLayout) findViewById(R.id.keepPhoto);
keepPhoto.setVisibility(View.VISIBLE);
// Hide the capture button.
Button photo = (Button) findViewById(R.id.capture);
photo.setVisibility(View.INVISIBLE);
// Hide the zoom progressBar
SeekBar zoomLevel = (SeekBar) findViewById(R.id.zoomLevel);
zoomLevel.setVisibility(View.INVISIBLE);
// Put button miniature at the top of the page
Button miniature = (Button) findViewById(R.id.miniature);
LayoutParams paramsLayoutAcceptDecline = (LinearLayout.LayoutParams) miniature
.getLayoutParams();
((LinearLayout.LayoutParams) paramsLayoutAcceptDecline).gravity = Gravity.TOP;
miniature.setLayoutParams(paramsLayoutAcceptDecline);
photoTaken = true;
// If miniature mode when photo is taken, the miniature goes to
// the top
if (modeMiniature) {
ImageView background = (ImageView) findViewById(R.id.background);
setParamsMiniature(background, false);
}
// Stop link between view and camera to start the preview // Stop link between view and camera to start the preview
// picture. // picture.
customCamera.stopPreview(); customCamera.stopPreview();
@@ -452,19 +462,126 @@ public class CameraActivity extends Activity {
} }
// Preview from camera // Preview from camera
storedBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt); photoTaken = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
// Show buttons to accept or decline the picture.
LinearLayout keepPhoto = (LinearLayout) findViewById(R.id.keepPhoto);
keepPhoto.setVisibility(View.VISIBLE);
// Hide the capture button.
Button photo = (Button) findViewById(R.id.capture);
photo.setVisibility(View.INVISIBLE);
// Hide the zoom progressBar
SeekBar zoomLevel = (SeekBar) findViewById(R.id.zoomLevel);
zoomLevel.setVisibility(View.INVISIBLE);
// Put button miniature at the top of the page
Button miniature = (Button) findViewById(R.id.miniature);
LayoutParams paramsLayoutMiniature = (LinearLayout.LayoutParams) miniature
.getLayoutParams();
((LinearLayout.LayoutParams) paramsLayoutMiniature).gravity = Gravity.TOP;
miniature.setLayoutParams(paramsLayoutMiniature);
// If miniature mode when photo is taken, the miniature goes to
// the top
if (modeMiniature) {
ImageView background = (ImageView) findViewById(R.id.background);
setParamsMiniature(background, false);
}
// Lock the screen until the accept/decline button is clicked // Lock the screen until the accept/decline button is clicked
// ---> OPTION // ---> OPTION
// lockScreen(true); // lockScreen(true);
assignFunctionButtonAcceptAndDecline(); // assignFunctionButtonAcceptAndDecline();
} }
}; };
// Start capture picture. // Start capture picture.
customCamera.takePicture(shutterCallback, rawCallback, jpegCallback); customCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
} }
/**
* Call when the photo is accepted.
* @param view The curretnView.
*/
public void acceptPhoto(View view) {
final CameraActivity cameraActivityCurrent = this;
try {
// Matrix to perform rotation
Matrix mat = new Matrix();
float redirect = redirectPhotoInGallery();
mat.postRotate(redirect);
// Creation of the bitmap
photoTaken = Bitmap.createBitmap(photoTaken, 0, 0,
photoTaken.getWidth(), photoTaken.getHeight(), mat, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photoTaken.compress(CompressFormat.JPEG, 70, stream);
// Get path picture to storage.
String pathPicture = Environment.getExternalStorageDirectory()
.getPath() + "/" + Environment.DIRECTORY_DCIM + "/Camera/";
pathPicture = pathPicture
+ String.format("%d.jpeg", System.currentTimeMillis());
// Write data in file.
FileOutputStream outStream = new FileOutputStream(pathPicture);
outStream.write(stream.toByteArray());
outStream.close();
// Return to success & finish current activity.
cameraActivityCurrent.setResult(1,
new Intent().putExtra("pathPicture", pathPicture));
cameraActivityCurrent.finish();
// Unlock the screen ---> OPTION
// lockScreen(false);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Call when the photo is declined.
* @param view The current View.
*/
public void declinePhoto(View view) {
final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
final LinearLayout keepPhoto = (LinearLayout) findViewById(R.id.keepPhoto);
final LinearLayout buttonMiniatureAndPhoto =
(LinearLayout) findViewById(R.id.buttonMiniatureAndPhoto);
final Button miniature = (Button) findViewById(R.id.miniature);
final Button photo = (Button) findViewById(R.id.capture);
photoTaken = null;
LayoutParams paramsLayoutMiniature = (LinearLayout.LayoutParams) miniature
.getLayoutParams();
((LinearLayout.LayoutParams) paramsLayoutMiniature).gravity = Gravity.BOTTOM;
miniature.setLayoutParams(paramsLayoutMiniature);
// If mode miniature and photo is declined, the miniature goes
// back to the bottom
if (modeMiniature) {
ImageView background = (ImageView) findViewById(R.id.background);
setParamsMiniature(background, false);
}
keepPhoto.setVisibility(View.INVISIBLE);
photo.setVisibility(View.VISIBLE);
SeekBar zoomLevel = (SeekBar) findViewById(R.id.zoomLevel);
zoomLevel.setVisibility(View.VISIBLE);
ImageView photoResized = (ImageView) findViewById(R.id.photoResized);
photoResized.setVisibility(View.INVISIBLE);
customCamera.startPreview();
preview.setVisibility(View.VISIBLE);
buttonMiniatureAndPhoto.setVisibility(View.VISIBLE);
// Unlock the screen ---> OPTION
// lockScreen(false);
}
/** /**
* To redirect (by rotation) the image stored in gallery. * To redirect (by rotation) the image stored in gallery.
* *
@@ -476,31 +593,29 @@ public class CameraActivity extends Activity {
int redirect = 0; int redirect = 0;
int orientationCamera = getOrientationOfCamera(); int orientationCamera = getOrientationOfCamera();
if (defaultOrientation == 1 || defaultOrientation == 2) { switch (windowManager.getDefaultDisplay().getRotation()) {
switch (windowManager.getDefaultDisplay().getRotation()) { case 0:
case 0: redirect = (defaultOrientation == Configuration.ORIENTATION_PORTRAIT) ? 90 : 0;
redirect = (defaultOrientation == 1) ? 90 : 0; // If the device is in front camera by default
// If the device is in front camera by default if (orientationCamera == 1 && defaultOrientation == Configuration.ORIENTATION_PORTRAIT) {
if (orientationCamera == 1 && defaultOrientation == 1) { redirect = 270;
redirect = 270; }
} break;
break; case 1:
case 1: redirect = (defaultOrientation == Configuration.ORIENTATION_PORTRAIT) ? 0 : 270;
redirect = (defaultOrientation == 1) ? 0 : 270; break;
break; case 2:
case 2: redirect = (defaultOrientation == Configuration.ORIENTATION_PORTRAIT) ? 270 : 180;
redirect = (defaultOrientation == 1) ? 270 : 180; // If the device is in front camera by default
// If the device is in front camera by default if (orientationCamera == 1 && defaultOrientation == Configuration.ORIENTATION_PORTRAIT) {
if (orientationCamera == 1 && defaultOrientation == 1) { redirect = 90;
redirect = 90; }
} break;
break; case 3:
case 3: redirect = (defaultOrientation == Configuration.ORIENTATION_PORTRAIT) ? 180 : 90;
redirect = (defaultOrientation == 1) ? 180 : 90; break;
break; default:
default: break;
break;
}
} }
return redirect; return redirect;
@@ -538,62 +653,11 @@ public class CameraActivity extends Activity {
return true; return true;
} }
/** When the back button is pressed. */
@Override
public void onBackPressed() {
this.setResult(3);
this.finish();
}
/** To set background in the view. */
protected void setBackground() {
// Get the base64 picture for the background only if it's exist.
byte[] imgBackgroundBase64 = TransferBigData.getImgBackgroundBase64();
if (imgBackgroundBase64 != null) {
// Get picture.
Bitmap imgBackgroundBitmap = BitmapFactory.decodeByteArray(
imgBackgroundBase64, 0, imgBackgroundBase64.length);
// Get sizes screen.
Display defaultDisplay = getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
defaultDisplay.getMetrics(displayMetrics);
int displayWidthPx = (int) displayMetrics.widthPixels;
int displayHeightPx = (int) displayMetrics.heightPixels;
// Get sizes picture.
int widthBackground = (int) (imgBackgroundBitmap.getWidth() * displayMetrics.density);
int heightBackground = (int) (imgBackgroundBitmap.getHeight() * displayMetrics.density);
// Change size ImageView.
RelativeLayout.LayoutParams paramsMiniature = new RelativeLayout.LayoutParams(
widthBackground, heightBackground);
float ratioX = (float) displayWidthPx / (float) widthBackground;
float ratioY = (float) displayHeightPx / (float) heightBackground;
if (ratioX < ratioY && ratioX < 1) {
paramsMiniature.width = (int) displayWidthPx;
paramsMiniature.height = (int) (ratioX * heightBackground);
} else if (ratioX >= ratioY && ratioY < 1) {
paramsMiniature.width = (int) (ratioY * widthBackground);
paramsMiniature.height = (int) displayHeightPx;
}
// set image at the view.
ImageView background = (ImageView) findViewById(R.id.background);
background.setImageBitmap(imgBackgroundBitmap);
paramsMiniature.addRule(RelativeLayout.CENTER_IN_PARENT,
RelativeLayout.TRUE);
background.setLayoutParams(paramsMiniature);
}
}
/** To save some contains of the activity. */ /** To save some contains of the activity. */
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("photoTaken", photoTaken);
outState.putBoolean("modeMiniature", modeMiniature); outState.putBoolean("modeMiniature", modeMiniature);
outState.putParcelable("storedBitmap", storedBitmap); outState.putParcelable("photoTaken", photoTaken);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@@ -604,201 +668,235 @@ public class CameraActivity extends Activity {
LinearLayout keepPhoto = (LinearLayout) findViewById(R.id.keepPhoto); LinearLayout keepPhoto = (LinearLayout) findViewById(R.id.keepPhoto);
LinearLayout buttonMiniatureAndPhoto = LinearLayout buttonMiniatureAndPhoto =
(LinearLayout) findViewById(R.id.buttonMiniatureAndPhoto); (LinearLayout) findViewById(R.id.buttonMiniatureAndPhoto);
photoTaken = savedInstanceState.getBoolean("photoTaken");
modeMiniature = savedInstanceState.getBoolean("modeMiniature"); modeMiniature = savedInstanceState.getBoolean("modeMiniature");
storedBitmap = savedInstanceState.getParcelable("storedBitmap"); photoTaken = savedInstanceState.getParcelable("photoTaken");
if (modeMiniature) {
buttonMiniature(findViewById(R.id.miniature));
}
// If the photo is taken when we orient the device // If the photo is taken when we orient the device
if (photoTaken) { if (photoTaken != null) {
// Matrix to perform rotation // Matrix to perform rotation
Matrix mat = new Matrix(); Matrix mat = new Matrix();
mat.postRotate(90); mat.postRotate(90);
// Creation of the bitmap // Creation of the bitmap
storedBitmap = Bitmap.createBitmap(storedBitmap, 0, 0, photoTaken = Bitmap.createBitmap(photoTaken, 0, 0,
storedBitmap.getWidth(), storedBitmap.getHeight(), mat, true); photoTaken.getWidth(), photoTaken.getHeight(), mat, true);
Bitmap newBitmap = resizeAfterRotate(storedBitmap); Bitmap newBitmap = resizeAfterRotate(photoTaken);
ImageView photoResized = (ImageView) findViewById(R.id.photoResized); ImageView photoResized = (ImageView) findViewById(R.id.photoResized);
photoResized.setImageBitmap(newBitmap); photoResized.setImageBitmap(newBitmap);
preview.setVisibility(View.INVISIBLE); preview.setVisibility(View.INVISIBLE);
keepPhoto.setVisibility(View.VISIBLE); keepPhoto.setVisibility(View.VISIBLE);
buttonMiniatureAndPhoto.setVisibility(View.INVISIBLE); buttonMiniatureAndPhoto.setVisibility(View.INVISIBLE);
assignFunctionButtonAcceptAndDecline(); // assignFunctionButtonAcceptAndDecline();
} }
super.onRestoreInstanceState(savedInstanceState); super.onRestoreInstanceState(savedInstanceState);
} }
/** /**
* Resize the bitmap saved when you rotate the device. * Resize the bitmap saved when you rotate the device.
* * @param Bitmap bitmap The original bitmap
* @param Bitmap bitmap The original bitmap *
* * @return the new bitmap.
* @return the new bitmap. */
*/
protected Bitmap resizeAfterRotate(Bitmap bitmap) { protected Bitmap resizeAfterRotate(Bitmap bitmap) {
// Initialize the new bitmap resized // Initialize the new bitmap resized
Bitmap newBitmap = null; Bitmap newBitmap = null;
// Get sizes screen. // Get sizes screen.
Display defaultDisplay = getWindowManager().getDefaultDisplay(); Display defaultDisplay = getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics(); DisplayMetrics displayMetrics = new DisplayMetrics();
defaultDisplay.getMetrics(displayMetrics); defaultDisplay.getMetrics(displayMetrics);
int displayWidthPx = (int) displayMetrics.widthPixels; int displayWidthPx = (int) displayMetrics.widthPixels;
int displayHeightPx = (int) displayMetrics.heightPixels; int displayHeightPx = (int) displayMetrics.heightPixels;
// Get sizes picture. // Get sizes picture.
int widthBackground = (int) (bitmap.getWidth() * displayMetrics.density); int widthBackground = (int) (bitmap.getWidth() * displayMetrics.density);
int heightBackground = (int) (bitmap.getHeight() * displayMetrics.density); int heightBackground = (int) (bitmap.getHeight() * displayMetrics.density);
// Change size ImageView. // Change size ImageView.
float ratioX = (float) displayWidthPx / (float) widthBackground; float ratioX = (float) displayWidthPx / (float) widthBackground;
float ratioY = (float) displayHeightPx / (float) heightBackground; float ratioY = (float) displayHeightPx / (float) heightBackground;
if (ratioX < ratioY && ratioX < 1) { if (ratioX < ratioY && ratioX < 1) {
newBitmap = Bitmap.createScaledBitmap(bitmap, (int) displayWidthPx, newBitmap = Bitmap.createScaledBitmap(bitmap, (int) displayWidthPx,
(int) (ratioX * heightBackground), false); (int) (ratioX * heightBackground), false);
} else if (ratioX >= ratioY && ratioY < 1) { } else if (ratioX >= ratioY && ratioY < 1) {
newBitmap = Bitmap.createScaledBitmap(bitmap, newBitmap = Bitmap.createScaledBitmap(bitmap,
(int) (ratioY * widthBackground), (int) displayHeightPx, false); (int) (ratioY * widthBackground), (int) displayHeightPx, false);
} }
return newBitmap; return newBitmap;
} }
/**
* Allow to lock the screen or not.
*
* @param boolean lock Do we have to lock or not ?
*/
protected void lockScreen(boolean lock) {
if (lock == false) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
} else {
Display defaultDisplay = getWindowManager().getDefaultDisplay();
int defaultOrientation = this.getDeviceDefaultOrientation();
if (defaultOrientation == Configuration.ORIENTATION_LANDSCAPE) {
switch (defaultDisplay.getRotation()) {
case 0:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case 1:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case 2:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case 3:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
default:
break;
}
} else if (defaultOrientation == Configuration.ORIENTATION_PORTRAIT) { // /**
switch (defaultDisplay.getRotation()) { // * Display the miniature.
case 0: // *
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // * @param view Current view.
break; // */
case 1: // public void showMiniature(View view) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // ImageView background = (ImageView) findViewById(R.id.background);
break; // final Button miniature = (Button) view;
case 2: //
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); // // if it's not miniature mode.
break; // if (!modeMiniature) {
case 3: // modeMiniature = true;
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); // // Set new size for miniature layout.
break; // setParamsMiniature(background, true);
default: //
break; // // Hide the miniature button.
} // miniature.setVisibility(View.INVISIBLE);
} // // Add event on click action for the miniature picture.
// background.setOnClickListener(new View.OnClickListener() {
// public void onClick(View view) {
// modeMiniature = false;
// ImageView background = (ImageView) findViewById(R.id.background);
// // resize miniature.
// background.setClickable(false);
// setBackground();
//
// miniature.setVisibility(View.VISIBLE);
// }
// });
// }
// }
}
}
/** Assign actions on button "accept" and "decline". */ // /** When the back button is pressed. */
protected void assignFunctionButtonAcceptAndDecline() { // @Override
final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); // public void onBackPressed() {
final LinearLayout keepPhoto = (LinearLayout) findViewById(R.id.keepPhoto); // this.setResult(3);
final LinearLayout buttonMiniatureAndPhoto = // this.finish();
(LinearLayout) findViewById(R.id.buttonMiniatureAndPhoto); // }
final Button miniature = (Button) findViewById(R.id.miniature);
final Button accept = (Button) findViewById(R.id.accept);
final Button decline = (Button) findViewById(R.id.decline);
final Button photo = (Button) findViewById(R.id.capture);
final CameraActivity cameraActivityCurrent = this;
// Event started after accept picture.
accept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
// Matrix to perform rotation
Matrix mat = new Matrix();
float redirect = redirectPhotoInGallery();
mat.postRotate(redirect);
// Creation of the bitmap // /**
storedBitmap = Bitmap.createBitmap(storedBitmap, 0, 0, // * Allow to lock the screen or not.
storedBitmap.getWidth(), storedBitmap.getHeight(), mat, true); // *
ByteArrayOutputStream stream = new ByteArrayOutputStream(); // * @param boolean lock Do we have to lock or not ?
storedBitmap.compress(CompressFormat.JPEG, 70, stream); // */
// protected void lockScreen(boolean lock) {
photoTaken = false; // if (lock == false) {
// Get path picture to storage. // this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
String pathPicture = Environment.getExternalStorageDirectory() // } else {
.getPath() + "/" + Environment.DIRECTORY_DCIM + "/Camera/"; // int defaultOrientation = this.getDeviceDefaultOrientation();
pathPicture = pathPicture // int newOrientation = 0;
+ String.format("%d.jpeg", System.currentTimeMillis()); //
// switch (getWindowManager().getDefaultDisplay().getRotation()) {
// Write data in file. // case 0:
FileOutputStream outStream = new FileOutputStream(pathPicture); // newOrientation = defaultOrientation == Configuration.ORIENTATION_LANDSCAPE
outStream.write(stream.toByteArray()); // ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
outStream.close(); // : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
// break;
// Return to success & finish current activity. // case 1:
cameraActivityCurrent.setResult(1, // newOrientation = defaultOrientation == Configuration.ORIENTATION_LANDSCAPE
new Intent().putExtra("pathPicture", pathPicture)); // ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
cameraActivityCurrent.finish(); // : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
// break;
// Unlock the screen ---> OPTION // case 2:
// lockScreen(false); // newOrientation = defaultOrientation == Configuration.ORIENTATION_LANDSCAPE
} catch (IOException e) { // ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
e.printStackTrace(); // : ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
} // break;
} // case 3:
}); // newOrientation = defaultOrientation == Configuration.ORIENTATION_LANDSCAPE
// ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
// Event started after decline picture. // : ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
decline.setOnClickListener(new View.OnClickListener() { // break;
@Override // default:
public void onClick(View view) { // break;
photoTaken = false; // }
LayoutParams paramsLayoutAcceptDecline = (LinearLayout.LayoutParams) miniature //
.getLayoutParams(); // this.setRequestedOrientation(newOrientation);
((LinearLayout.LayoutParams) paramsLayoutAcceptDecline).gravity = Gravity.BOTTOM; // }
miniature.setLayoutParams(paramsLayoutAcceptDecline); // }
//
// If mode miniature and photo is declined, the miniature goes // /** Assign actions on button "accept" and "decline". */
// back to the bottom // protected void assignFunctionButtonAcceptAndDecline() {
if (modeMiniature) { // final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
ImageView background = (ImageView) findViewById(R.id.background); // final LinearLayout keepPhoto = (LinearLayout) findViewById(R.id.keepPhoto);
setParamsMiniature(background, false); // final LinearLayout buttonMiniatureAndPhoto =
} // (LinearLayout) findViewById(R.id.buttonMiniatureAndPhoto);
keepPhoto.setVisibility(View.INVISIBLE); // final Button miniature = (Button) findViewById(R.id.miniature);
photo.setVisibility(View.VISIBLE); // final Button accept = (Button) findViewById(R.id.accept);
SeekBar zoomLevel = (SeekBar) findViewById(R.id.zoomLevel); // final Button decline = (Button) findViewById(R.id.decline);
zoomLevel.setVisibility(View.VISIBLE); // final Button photo = (Button) findViewById(R.id.capture);
ImageView photoResized = (ImageView) findViewById(R.id.photoResized); // final CameraActivity cameraActivityCurrent = this;
photoResized.setVisibility(View.INVISIBLE); //
customCamera.startPreview(); // // Event started after accept picture.
preview.setVisibility(View.VISIBLE); // accept.setOnClickListener(new View.OnClickListener() {
buttonMiniatureAndPhoto.setVisibility(View.VISIBLE); // @Override
// public void onClick(View view) {
// Unlock the screen ---> OPTION // try {
// lockScreen(false); // // Matrix to perform rotation
} // Matrix mat = new Matrix();
}); // float redirect = redirectPhotoInGallery();
} // mat.postRotate(redirect);
//
// // Creation of the bitmap
// storedBitmap = Bitmap.createBitmap(storedBitmap, 0, 0,
// storedBitmap.getWidth(), storedBitmap.getHeight(), mat, true);
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
// storedBitmap.compress(CompressFormat.JPEG, 70, stream);
//
// photoTaken = false;
// // Get path picture to storage.
// String pathPicture = Environment.getExternalStorageDirectory()
// .getPath() + "/" + Environment.DIRECTORY_DCIM + "/Camera/";
// pathPicture = pathPicture
// + String.format("%d.jpeg", System.currentTimeMillis());
//
// // Write data in file.
// FileOutputStream outStream = new FileOutputStream(pathPicture);
// outStream.write(stream.toByteArray());
// outStream.close();
//
// // Return to success & finish current activity.
// cameraActivityCurrent.setResult(1,
// new Intent().putExtra("pathPicture", pathPicture));
// cameraActivityCurrent.finish();
//
// // Unlock the screen ---> OPTION
// // lockScreen(false);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// });
//
// // Event started after decline picture.
// decline.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// photoTaken = false;
// LayoutParams paramsLayoutAcceptDecacceptedline = (LinearLayout.LayoutParams) miniature
// .getLayoutParams();
// ((LinearLayout.LayoutParams) paramsLayoutAcceptDecline).gravity = Gravity.BOTTOM;
// miniature.setLayoutParams(paramsLayoutAcceptDecline);
//
// // If mode miniature and photo is declined, the miniature goes
// // back to the bottom
// if (modeMiniature) {
// ImageView background = (ImageView) findViewById(R.id.background);
// setParamsMiniature(background, false);
// }
// keepPhoto.setVisibility(View.INVISIBLE);
// photo.setVisibility(View.VISIBLE);
// SeekBar zoomLevel = (SeekBar) findViewById(R.id.zoomLevel);
// zoomLevel.setVisibility(View.VISIBLE);
// ImageView photoResized = (ImageView) findViewById(R.id.photoResized);
// photoResized.setVisibility(View.INVISIBLE);
// customCamera.startPreview();
// preview.setVisibility(View.VISIBLE);
// buttonMiniatureAndPhoto.setVisibility(View.VISIBLE);
//
// // Unlock the screen ---> OPTION
// // lockScreen(false);
// }
// });
// }
} }