public class CameraPic extends Activity implements SurfaceHolder.Callback,
ErrorCallback {
Camera camera;
SurfaceView surfaceView1;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
Button btnPicture, btnCancel;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
final int NONE = 0;
final int DRAG = 1;
final int ZOOM = 2;
int mode = NONE;
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
private static final String TAG = "Touch";
static Bitmap resizedBitmap;
int resizewidth, resizeheight, orientation;
Bitmap bMapRotate, bMap;
static int cameraPicStatus = 0;
static int cameraWigStatus = 0;
float[] lastEvent = null;
float degree = 0f;
float newRot = 0f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
surfaceView1 = (SurfaceView) findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView1.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View v = controlInflater.inflate(R.layout.customcameralayout, null);
LayoutParams layoutParamsControl = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
btnPicture = (Button) v.findViewById(R.id.btnPicture);
btnCancel = (Button) v.findViewById(R.id.btnCancel);
this.addContentView(v, layoutParamsControl);
btnPicture.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
camera.takePicture(shutter, picture1, picture2);
}
});
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
ShutterCallback shutter = new ShutterCallback() {
public void onShutter() {
}
};
PictureCallback picture1 = new PictureCallback() {
public void onPictureTaken(byte[] arg0, Camera arg1) {
}
};
PictureCallback picture2 = new PictureCallback() {
public void onPictureTaken(byte[] data, final Camera camera) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[32 * 1024];
bMap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
if (bMap.getHeight() < bMap.getWidth()) {
orientation = 90;
} else {
orientation = 0;
}
if (orientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),
bMap.getHeight(), matrix, true);
} else
bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
bMap.getHeight(), true);
camera.startPreview();
final Dialog d = new Dialog(CameraPic.this);
d.setTitle(R.string.app_name);
d.setCancelable(true);
d.setContentView(R.layout.customphotoedit);
final ImageView imgViewEditPhoto = (ImageView) d
.findViewById(R.id.imgViewEditPhoto);
final Button btnDone = (Button) d.findViewById(R.id.btnDone);
final Button btnCancel = (Button) d.findViewById(R.id.btnCancel);
imgViewEditPhoto.setImageBitmap(bMapRotate);
btnDone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Background.galleryStatus = 0;
imgViewEditPhoto.buildDrawingCache();
resizedBitmap = imgViewEditPhoto.getDrawingCache();
cameraPicStatus = 1;
cameraWigStatus = 1;
clearBitmap(resizedBitmap);
Intent intent = new Intent();
intent.setClass(CameraPic.this, AnotherActivity.class);
startActivity(intent);
}
});
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
d.cancel();
}
});
imgViewEditPhoto.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
resizewidth = bMapRotate.getWidth();
resizeheight = bMapRotate.getHeight();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
lastEvent = null;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
lastEvent = null;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
lastEvent = new float[4];
lastEvent[0] = event.getX(0);
lastEvent[1] = event.getX(1);
lastEvent[2] = event.getY(0);
lastEvent[3] = event.getY(1);
degree = rotation(event);
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
if (lastEvent != null) {
newRot = rotation(event);
float r = newRot - degree;
matrix.postRotate(
r,
imgViewEditPhoto.getMeasuredWidth() / 2,
imgViewEditPhoto.getMeasuredHeight() / 2);
}
}
break;
}
imgViewEditPhoto.setImageMatrix(matrix);
return true;
}
private float rotation(MotionEvent event) {
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
});
d.show();
}
};
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
camera.startPreview();
}
public void surfaceCreated(SurfaceHolder holder) {
if (camera == null) {
camera = Camera.open();
try {
Camera.Parameters p = camera.getParameters();
if (Integer.parseInt(Build.VERSION.SDK) >= 8)
setDisplayOrientation(camera, 90);
else {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
p.set("orientation", "portrait");
p.set("rotation", 90);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
p.set("orientation", "landscape");
p.set("rotation", 90);
}
}
camera.setParameters(p);
camera.setPreviewDisplay(holder);
} catch (IOException e) {
camera.release();
camera = null;
}
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
protected void setDisplayOrientation(Camera camera, int angle) {
Method downPolymorphic;
try {
downPolymorphic = camera.getClass().getMethod(
"setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
} catch (Exception e1) {
}
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
public static void clearBitmap(Bitmap bm) {
// bm.recycle();
System.gc();
}
public static boolean createDirIfNotExists(String path) {
boolean ret = true;
File file = new File(Environment.getExternalStorageDirectory(), path);
if (!file.exists()) {
if (!file.mkdirs()) {
// Log.e("TravellerLog :: ", "Problem creating Image folder");
ret = false;
}
}
return ret;
}
public static Bitmap getBitmap() {
return resizedBitmap;
}
public void onError(int arg0, Camera arg1) {
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
}
0 comments: