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 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>