public class CameraActivity extends Activity implements View.onTouchListener,OnClick(){
public static final String CAMERA_PATH_VALUE1 = "PHOTO_PATH";
public static final String CAMERA_PATH_VALUE2 = "PATH";public static final String CAMERA_TYPE = "CAMERA_TYPE";
public static final String CAMERA_RETURN_PATH = "return_path";
private int PHOTO_SIZE_W = 2000;
private int PHOTO_SIZE_H = 2000;
public static final int CAMERA_TYPE_1 = 1;
public static final int CAMERA_TYPE_2 = 2;
private final int PROCESS = 1;
private CameraPreview preview;
private Camera camera;
private Context mContext;
private View focusIndex;
private ImageView flashBtn;
private int mCurrentCameraId = 0; // 1是前置 0是后置
private SurfaceView mSurfaceView;
private CameraGrid mCameraGrid;
private int type = 1; //引用的矩形框
private ImageView mBtnSearch;
private Button mBtnTakePhoto;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
//requestWindowFeature(Window.FEATURE_NO_TITLE);
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//拍照过程屏幕一直处于高亮
setContentView(R.layout.camera_home);
type = getIntent().getIntExtra(CAMERA_TYPE, CAMERA_TYPE_2);
initView();
InitData();
}
private void initView() {
focusIndex = (View) findViewById(R.id.focus_index);
flashBtn = (ImageView) findViewById(R.id.flash_view);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
mCameraGrid = (CameraGrid) findViewById(R.id.camera_grid);
mBtnSearch = (ImageView) findViewById(R.id.search);
mBtnTakePhoto = (Button) findViewById(R.id.takephoto);
}
private void InitData() {
preview = new CameraPreview(this, mSurfaceView);
preview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
((FrameLayout) findViewById(R.id.layout)).addView(preview);
preview.setKeepScreenOn(true);
mSurfaceView.setOnTouchListener(this);
mCameraGrid.setType(type);
}
private Handler handler = new Handler();
private void takePhoto() {
try {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
} catch (Throwable t) {
t.printStackTrace();
Toast.makeText(getApplication(), "拍照失败,请重试!", Toast.LENGTH_LONG)
.show();
try {
camera.startPreview();
} catch (Throwable e) {
}
}
}
@Override
protected void onResume() {
super.onResume();
int numCams = Camera.getNumberOfCameras();
if (numCams > 0) {
try {
mCurrentCameraId = 0;
camera = Camera.open(mCurrentCameraId);
camera.startPreview();
preview.setCamera(camera);
preview.reAutoFocus();
} catch (RuntimeException ex) {
Toast.makeText(mContext, "未发现相机", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onPause() {
if (camera != null) {
camera.stopPreview();
preview.setCamera(null);
camera.release();
camera = null;
preview.setNull();
}
super.onPause();
}
private void resetCam() {
camera.startPreview();
preview.setCamera(camera);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SaveImageTask(data).execute();
resetCam();
}
};
@Override
public boolean onTouch(View v, MotionEvent event) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
preview.pointFocus(event);
}
} catch (Exception e) {
e.printStackTrace();
}
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(
focusIndex.getLayoutParams());
layout.setMargins((int) event.getX() - 60, (int) event.getY() - 60, 0,0);
focusIndex.setLayoutParams(layout);
focusIndex.setVisibility(View.VISIBLE);
ScaleAnimation sa = new ScaleAnimation(3f, 1f, 3f, 1f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(800);
focusIndex.startAnimation(sa);
handler.postAtTime(new Runnable() {
@Override
public void run() {
focusIndex.setVisibility(View.INVISIBLE);
}
}, 800);
return false;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
/*case R.id.camera_back:
setResult(0);
finish();
break;*/
case R.id.camera_flip_view:
switchCamera();
break;
case R.id.flash_view:
turnLight(camera);
break;
case R.id.action_button:
takePhoto();
break;
case R.id.search: //处理选中状态
mBtnSearch.setSelected(true);
mBtnTakePhoto.setSelected(false);
break;
case R.id.takephoto: //延迟3秒拍照
// mBtnTakePhoto.setSelected(true);
// mBtnSearch.setSelected(false);
new Handler().postDelayed(new Runnable(){
public void run() {
//execute the task
takePhoto();
}
}, 3000);
break;
}
}
private static String getCameraPath() {
Calendar calendar = Calendar.getInstance();
StringBuilder sb = new StringBuilder();
sb.append("IMG");
sb.append(calendar.get(Calendar.YEAR));
int month = calendar.get(Calendar.MONTH) + 1; // 0~11
sb.append(month < 10 ? "0" + month : month);
int day = calendar.get(Calendar.DATE);
sb.append(day < 10 ? "0" + day : day);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
sb.append(hour < 10 ? "0" + hour : hour);
int minute = calendar.get(Calendar.MINUTE);
sb.append(minute < 10 ? "0" + minute : minute);
int second = calendar.get(Calendar.SECOND);
sb.append(second < 10 ? "0" + second : second);
if (!new File(sb.toString() + ".jpg").exists()) {
return sb.toString() + ".jpg";
}
StringBuilder tmpSb = new StringBuilder(sb);
int indexStart = sb.length();
for (int i = 1; i < Integer.MAX_VALUE; i++) {
tmpSb.append('(');
tmpSb.append(i);
tmpSb.append(')');
tmpSb.append(".jpg");
if (!new File(tmpSb.toString()).exists()) {
break;
}
tmpSb.delete(indexStart, tmpSb.length());
}
return tmpSb.toString();
}
//处理拍摄的照片
private class SaveImageTask extends AsyncTask<Void, Void, String> {
private byte[] data;
SaveImageTask(byte[] data) {
this.data = data;
}
@Override
protected String doInBackground(Void... params) {
// Write to SD Card
String path = "";
try {
showProgressDialog("处理中");
path = saveToSDCard(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return path;
}
@Override
protected void onPostExecute(String path) {
super.onPostExecute(path);
if (!TextUtils.isEmpty(path)) {
Log.d("DemoLog", "path=" + path);
dismissProgressDialog();
mBtnSearch.setImageBitmap(getImageThumbnail(path,60,60));
Intent intent = new Intent();//跳转到显示界面
intent.setClass(CameraActivity.this, PhotoProcessActivity.class);
intent.putExtra(CAMERA_PATH_VALUE1, path);
startActivityForResult(intent, PROCESS);
} else {
Toast.makeText(getApplication(), "拍照失败,请稍后重试!",
Toast.LENGTH_LONG).show();
}
}
}
private AlertDialog mAlertDialog;
private void dismissProgressDialog() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mAlertDialog != null && mAlertDialog.isShowing()
&& !CameraActivity.this.isFinishing()) {
mAlertDialog.dismiss();
mAlertDialog = null;
}
}
});
}
private void showProgressDialog(final String msg) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mAlertDialog == null) {
mAlertDialog = new GenericProgressDialog(
CameraActivity.this);
}
mAlertDialog.setMessage(msg);
((GenericProgressDialog) mAlertDialog)
.setProgressVisiable(true);
mAlertDialog.setCancelable(false);
mAlertDialog.setOnCancelListener(null);
mAlertDialog.show();
mAlertDialog.setCanceledOnTouchOutside(false);
}
});
}
/**
* 将拍下来的照片存放在SD卡中
*/
public String saveToSDCard(byte[] data) throws IOException {
Bitmap croppedImage;
// 获得图片大小
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
// PHOTO_SIZE = options.outHeight > options.outWidth ? options.outWidth
// : options.outHeight;
PHOTO_SIZE_W = options.outWidth;
PHOTO_SIZE_H = options.outHeight;
options.inJustDecodeBounds = false;
Rect r = new Rect(0, 0, PHOTO_SIZE_W, PHOTO_SIZE_H);
try {
croppedImage = decodeRegionCrop(data, r);
} catch (Exception e) {
return null;
}
String imagePath = "";
try {
imagePath = saveToFile(croppedImage);
} catch (Exception e) {
}
croppedImage.recycle();
return imagePath;
}
private Bitmap decodeRegionCrop(byte[] data, Rect rect) {
InputStream is = null;
System.gc();
Bitmap croppedImage = null;
try {
is = new ByteArrayInputStream(data);
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is,false);
try {
croppedImage = decoder.decodeRegion(rect,
new BitmapFactory.Options());
} catch (IllegalArgumentException e) {
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
}
Matrix m = new Matrix();
m.setRotate(90, PHOTO_SIZE_W / 2, PHOTO_SIZE_H / 2);
if (mCurrentCameraId == 1) {
m.postScale(1, -1);
}
Bitmap rotatedImage = Bitmap.createBitmap(croppedImage, 0, 0,
PHOTO_SIZE_W, PHOTO_SIZE_H, m, true);
if (rotatedImage != croppedImage)
croppedImage.recycle();
return rotatedImage;
}
// 保存图片文件
public static String saveToFile(Bitmap croppedImage)
throws FileNotFoundException, IOException {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/DCIM/Camera/");
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = getCameraPath();
File outFile = new File(dir, fileName);
System.out.println(dir+"/"+fileName);
FileOutputStream outputStream = new FileOutputStream(outFile); // 文件输出流
croppedImage.compress(Bitmap.CompressFormat.JPEG, 70, outputStream);
outputStream.flush();
outputStream.close();
return outFile.getAbsolutePath();
}
/**
* 闪光灯开关 开->关->自动
*
* @param mCamera
*/
private void turnLight(Camera mCamera) {
if (mCamera == null || mCamera.getParameters() == null
|| mCamera.getParameters().getSupportedFlashModes() == null) {
return;
}
Camera.Parameters parameters = mCamera.getParameters();
String flashMode = mCamera.getParameters().getFlashMode();
List<String> supportedModes = mCamera.getParameters()
.getSupportedFlashModes();
if (Camera.Parameters.FLASH_MODE_OFF.equals(flashMode)
&& supportedModes.contains(Camera.Parameters.FLASH_MODE_ON)) {// 关闭状态
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
mCamera.setParameters(parameters);
flashBtn.setImageResource(R.drawable.camera_flash_on);
} else if (Camera.Parameters.FLASH_MODE_ON.equals(flashMode)) {// 开启状态
if (supportedModes.contains(Camera.Parameters.FLASH_MODE_AUTO)) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
flashBtn.setImageResource(R.drawable.camera_flash_auto);
mCamera.setParameters(parameters);
} else if (supportedModes
.contains(Camera.Parameters.FLASH_MODE_OFF)) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
flashBtn.setImageResource(R.drawable.camera_flash_off);
mCamera.setParameters(parameters);
}
} else if (Camera.Parameters.FLASH_MODE_AUTO.equals(flashMode)
&& supportedModes.contains(Camera.Parameters.FLASH_MODE_OFF)) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
flashBtn.setImageResource(R.drawable.camera_flash_off);
}
}
// 切换前后置摄像头
private void switchCamera() {
mCurrentCameraId = (mCurrentCameraId + 1) % Camera.getNumberOfCameras();
if (camera != null) {
camera.stopPreview();
preview.setCamera(null);
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
try {
camera = Camera.open(mCurrentCameraId);
camera.setPreviewDisplay(mSurfaceView.getHolder());
preview.setCamera(camera);
camera.startPreview();
} catch (Exception e) {
Toast.makeText(mContext, "未发现相机", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
setResult(0);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PROCESS) {
if (resultCode == RESULT_OK) {
// Intent intent = new Intent();
// if (data != null) {
// intent.putExtra(CAMERA_RETURN_PATH,
// data.getStringExtra(CAMERA_PATH_VALUE2));
// }
// setResult(RESULT_OK, intent);
// finish();
} else {
if (data != null) {
File dir = new File(data.getStringExtra(CAMERA_PATH_VALUE2));
if (dir != null) {
dir.delete();
}
}
}
}
}
/**
* 根据指定的图像路径和大小来获取缩略图
* 此方法有两点好处:
* 1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
* 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。
* 2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使
* 用这个工具生成的图像不会被拉伸。
* @param imagePath 图像的路径
* @param width 指定输出图像的宽度
* @param height 指定输出图像的高度
* @return 生成的缩略图
*/
private Bitmap getImageThumbnail(String imagePath, int width, int height) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高,注意此处的bitmap为null
bitmap = BitmapFactory.decodeFile(imagePath, options);
options.inJustDecodeBounds = false; // 设为 false
// 计算缩放比
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / width;
int beHeight = h / height;
int be = 1;
if (beWidth < beHeight) {
be = beWidth;
} else {
be = beHeight;
}
if (be <= 0) {
be = 1;
}
options.inSampleSize = be;
// 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(imagePath, options);
// 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}
}
public class PhotoProcessActivity extends Activity implements View.OnClickListener {
private ImageView photoImageView;
private String path = "";
private TextView actionTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.photo_activity);
path = getIntent().getStringExtra(CameraActivity.CAMERA_PATH_VALUE1);
initView();
initData();
}
private void initData() {
Bitmap bitmap = null;
try {
bitmap = getImage(path);
//bitmap = loadBitmap(path, true);
} catch (Exception e) {
e.printStackTrace();
}
photoImageView.setImageBitmap(bitmap);
actionTextView.setOnClickListener(this);
}
private void initView() {
photoImageView = (ImageView) findViewById(R.id.photo_imageview);
actionTextView = (TextView) findViewById(R.id.photo_process_action);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
/**
* 从给定路径加载图片
*/
public static Bitmap loadBitmap(String imgpath) {
return BitmapFactory.decodeFile(imgpath);
}
/**
* 从给定的路径加载图片,并指定是否自动旋转方向
*/
public static Bitmap loadBitmap(String imgpath, boolean adjustOritation) throws OutOfMemoryError {
if (!adjustOritation) {
return loadBitmap(imgpath);
} else {
Bitmap bm = loadBitmap(imgpath);
int digree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(imgpath);
} catch (IOException e) {
e.printStackTrace();
exif = null;
}
if (exif != null) {
// 读取图片中相机方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
// 计算旋转角度
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
digree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
digree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
digree = 270;
break;
default:
digree = 0;
break;
}
}
if (digree != 0) {
// 旋转图片
Matrix m = new Matrix();
m.postRotate(digree);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
}
return bm;
}
}
private Bitmap getImage(String srcPath) throws OutOfMemoryError {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 800f;//这里设置高度为800f
float ww = 480f;//这里设置宽度为480f
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return bitmap;//压缩好比例大小后再进行质量压缩
}
private void refreshGallery(String file) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(new File(file)));
sendBroadcast(mediaScanIntent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_back:
refreshGallery(path);
Intent intentOk = new Intent();
intentOk.putExtra(CameraActivity.CAMERA_PATH_VALUE2, path);
setResult(RESULT_OK, intentOk);
finish();
break;
case R.id.photo_process_action:
Intent intent = new Intent();
intent.putExtra(CameraActivity.CAMERA_PATH_VALUE2, path);
setResult(0, intent);
finish();
break;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
// Intent intent = new Intent();
// intent.putExtra(CameraActivity.CAMERA_PATH_VALUE2, path);
// setResult(0, intent);
// finish();
// return true;
refreshGallery(path);
Intent intentOk = new Intent();
intentOk.putExtra(CameraActivity.CAMERA_PATH_VALUE2, path);
setResult(RESULT_OK, intentOk);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
public class CameraGrid extends View {
private int topBannerWidth = 0;
private Paint mPaint;
Context mContext;
private int type = 1;
public CameraGrid(Context context) {
this(context, null);
mContext = context;
}
public CameraGrid(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
public void setType(int type) {
this.type = type;
invalidate();
}
private void init() {
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAlpha(120);
mPaint.setStrokeWidth(1f);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = this.getWidth();
int height = this.getHeight();
if (width < height) {
topBannerWidth = height - width;
}
if (showGrid) {
canvas.drawLine(width / 3, 0, width / 3, height, mPaint);
canvas.drawLine(width * 2 / 3, 0, width * 2 / 3, height, mPaint);
canvas.drawLine(0, height / 3, width, height / 3, mPaint);
canvas.drawLine(0, height * 2 / 3, width, height * 2 / 3, mPaint);
}
if (type == CameraActivity.CAMERA_TYPE_1) {
Bitmap mBitmap = BitmapFactory.decodeResource(((Activity) mContext).getResources(), R.drawable.hold_id_card_ref);
canvas.drawBitmap(mBitmap, (width - mBitmap.getWidth()) / 2, dip2px(mContext, 50), mPaint);
} else if (type == CameraActivity.CAMERA_TYPE_2) {
// Bitmap mBitmap = BitmapFactory.decodeResource(((Activity) mContext).getResources(), R.drawable.frame_id_card02);
//canvas.drawBitmap(mBitmap, (width - mBitmap.getWidth()) / 2, (height - mBitmap.getHeight()) / 2, mPaint);
}
}
private boolean showGrid = false;
public boolean isShowGrid() {
return showGrid;
}
public void setShowGrid(boolean showGrid) {
this.showGrid = showGrid;
}
public int getTopWidth() {
return topBannerWidth;
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
public class GenericProgressDialog extends AlertDialog {
private ProgressBar mProgress;
private TextView mMessageView;
private CharSequence mMessage;
private boolean mIndeterminate;
private boolean mProgressVisiable;
public GenericProgressDialog(Context context) {
super(context/*,R.style.Float*/);
}
public GenericProgressDialog(Context context, int theme) {
super(context,/*, R.style.Float*/theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_dialog);
mProgress = (ProgressBar) findViewById(android.R.id.progress);
mMessageView = (TextView) findViewById(R.id.message);
setMessageAndView();
setIndeterminate(mIndeterminate);
}
private void setMessageAndView() {
mMessageView.setText(mMessage);
if (mMessage == null || "".equals(mMessage)) {
mMessageView.setVisibility(View.GONE);
}
mProgress.setVisibility(mProgressVisiable ? View.VISIBLE : View.GONE);
}
@Override
public void setMessage(CharSequence message) {
mMessage = message;
}
/**
* 圈圈可见性设置
* @param progressVisiable 是否显示圈圈
*/
public void setProgressVisiable(boolean progressVisiable) {
mProgressVisiable = progressVisiable;
}
public void setIndeterminate(boolean indeterminate) {
if (mProgress != null) {
mProgress.setIndeterminate(indeterminate);
} else {
mIndeterminate = indeterminate;
}
}
}
public class MyOrientationDetector extends OrientationEventListener {
int Orientation;
public MyOrientationDetector(Context context) {
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
this.Orientation = orientation;
}
public int getOrientation() {
return Orientation;
}
}
Canera_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CameraActivity" >
<!-- 预览画布 -->
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- 闪光灯、前置摄像头、后置摄像头、聚焦 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<org.gaochun.camera.CameraGrid
android:id="@+id/camera_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<View
android:id="@+id/focus_index"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/camera_focus"
android:visibility="invisible" />
<ImageView
android:id="@+id/flash_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:onClick="onClick"
android:padding="15dp"
android:scaleType="centerCrop"
android:src="@drawable/camera_flash_off" />
<ImageView
android:id="@+id/camera_flip_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="onClick"
android:padding="15dp"
android:scaleType="centerCrop"
android:src="@drawable/camera_flip" />
<!-- 底部按钮 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:background="#a0000000"
android:padding="5dp" >
<ImageView
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:background="@drawable/ic_launcher"
android:drawablePadding="3dp" />
<ImageView
android:id="@+id/action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:clickable="true"
android:onClick="onClick"
android:src="@drawable/btn_shutter_photo" />
<Button
android:id="@+id/takephoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:background="@null"
android:drawablePadding="3dp"
android:drawableTop="@drawable/ic_takephoto_selector"
android:onClick="onClick"
android:text="延时"
android:textColor="@drawable/row_selector_text" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
photo_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/photo_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/x" />
<RelativeLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<ImageView
android:id="@+id/iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:src="@drawable/back_arrow" />
<TextView
android:id="@+id/photo_process_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:text="删除"
android:textColor="#FFFFFF"
android:textSize="30dp" />
</RelativeLayout>
</RelativeLayout>
progress_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/simple_toast_bg" >
<LinearLayout
android:id="@+id/body"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="3dp"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:paddingTop="3dp" >
<ProgressBar
android:id="@android:id/progress"
android:layout_width="30dp"
android:layout_height="30dp"
android:max="10000" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="12dip"
android:textColor="#ffffff" />
</LinearLayout>
</FrameLayout>