package com.unicom.salemen.camera;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import com.unicom.salemen.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MyCamera extends Activity implements SurfaceHolder.Callback {
private static final String TAG = "salemen";
private static final String CAMERA_IMAGE_PATH = String.format("%s/%s",
Environment.getExternalStorageDirectory(), TAG);
private Camera mCamera;
private TelephonyManager tm;
private boolean mPreviewRunning = true;
private static final int ITEM0 = Menu.FIRST;
private static final int ITEM1 = Menu.FIRST + 1;
private static final int ITEM2 = Menu.FIRST + 2;
private byte[] data;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
private SurfaceView mSurfaceView;
private ImageButton shutter;
private ImageButton finish;
private SurfaceHolder mSurfaceHolder;
private String fileName;
/**
* 程序主界面
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().setFormat(PixelFormat.TRANSLUCENT);// 半透明
requestWindowFeature(Window.FEATURE_NO_TITLE);// 无标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏幕
setContentView(R.layout.camera_main);
checkMysoftStage();
findViews();
setListeners();
initMyCamera();
}
/**
* 实例化界面按钮
*/
private void findViews() {
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
shutter = (ImageButton) findViewById(R.id.shutter);
finish = (ImageButton) findViewById(R.id.finish);
}
/**
* 设置按钮事件
*/
private void setListeners() {
shutter.setOnClickListener(clickShootButton);
finish.setOnClickListener(clickExitButton);
}
/**
* 初始化组件
*/
public void initMyCamera() {
tm = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(MyCamera.this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ITEM1, 0, "设置");
menu.add(0, ITEM2, 0, "退出");
return true;
}
/**
* 拍照按钮事件
*/
private OnClickListener clickShootButton = new OnClickListener() {
@Override
public void onClick(View ic_camera_indicator_photo) {
mCamera.autoFocus(null);
mCamera.takePicture(mShutterCallback, null, mPictureCallback);// 第一个参数是拍照事件,第二个参数是原始数据流,第三个是jpeg格式流
}
};
/**
* 退出按钮事件
*/
private OnClickListener clickExitButton = new OnClickListener() {
@Override
public void onClick(View shezhi) {
MyCamera.super.openOptionsMenu();
}
};
/**
* 当预览界面的格式和大小发生改变时,该方法被调用
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mPreviewRunning) {
mCamera.stopPreview();
}
//Camera.Parameters p = mCamera.getParameters();
//p.setPreviewSize(w, h);
//mCamera.setParameters(p);
setCameraParameters();
mCamera.startPreview();
mPreviewRunning = true;
}
/**
* 创建Camera对象函数 初次实例化,界面打开时该方法自动调用
*/
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
setCameraParameters();
}
/**
* 设置摄像参数
*/
private void setCameraParameters() {
Camera.Parameters parameters = mCamera.getParameters();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
parameters.setPreviewSize(320, 427);//设置预览分辨率
parameters.setPictureSize(1536, 2048);//设置图片分辨率
mCamera.setParameters(parameters);
}
/**
* 销毁函数 当预览界面被关闭时,该方法被调用
*/
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mPreviewRunning = false;
mCamera.release();
mCamera = null;
}
/**
* 重点函数 此处实例化了本界面的PictureCallback
* 当用户拍完一张照片的时候触发onPictureTaken,这时候对拍下的照片进行相应的处理操作
*/
PictureCallback mPictureCallback = new PictureCallback() {
public void onPictureTaken(byte[] _data, Camera camera) {
if (_data != null) {
data = _data;
showDialog(1);
/*
* Intent intent = new Intent(); intent.setClass(MyCamera.this,
* PhotoView.class); Bundle bundle = new Bundle();
* bundle.putString("picPath",
* "/sdcard/fatalityUpload/fatality/" + fileName + ".jpg");
* intent.putExtras(bundle); startActivity(intent);
*/
}
}
};
Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
// 拍照发出声音等
}
};
private Dialog buildDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("是否储存照片");
builder.setPositiveButton("存储", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
savePicture();
mCamera.startPreview();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCamera.startPreview();
}
});
return builder.create();
};
public Dialog onCreateDialog(int i) {
switch (i) {
case 1: {
return buildDialog(MyCamera.this);
}
case 0: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("处理中,请稍后...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
/**
* 保存图片
*/
private void savePicture() {
try {
showDialog(0);
File dir = new File(CAMERA_IMAGE_PATH);
if (!dir.exists()) {
Log.d(TAG, "created folder" + dir.mkdirs());
}
long t = System.currentTimeMillis();
fileName = String.format("%s.jpg", t);
File f = new File(dir, fileName);
try {
f.createNewFile();
Bitmap bmp = BitmapFactory
.decodeByteArray(data, 0, data.length);
OutputStream os = new BufferedOutputStream(
new FileOutputStream(f));
bmp.compress(Bitmap.CompressFormat.JPEG, 85, os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
removeDialog(0);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* OptionsItem单击事件
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ITEM0:
break;
case ITEM1:
break;
case ITEM2:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* 检查一下手机设备各项硬件的开启状态
*/
public v