public class PhotographActivity extends BaseActivity implements
SeekBar.OnSeekBarChangeListener, OnClickListener, Runnable {
private SeekBar zoomSet;// 调整焦距
private ImageView takePic, back, flash;// 按钮
private final int FLASH_AUTO = 0;// 自动闪光
private final int FLASH_OFF = 1;// 闪光灯关闭
private final int FLASH_ON = 2; // 闪光灯打开
private int flashImg[] = { R.drawable.light_auto_confirm,
R.drawable.light_off_confirm, R.drawable.light_on_confirm };// 闪光灯图标
private int zoom, maxZoom;// zoom现在焦距,maxZoom最大焦距
private boolean preview = false, flashOpen = false;// preview预览 flashOpen闪光灯
private Camera camera;// 硬件相机
private SurfaceView cameraBG;// 相机预览
@SuppressWarnings("unused")
private Uri videoFilePath;
// PopupWindow和AlertDialog都是Android对话框的内容,不同的是PopupWindow是阻塞式对话框
PopupWindow popupWindow;
Camera.Parameters parameters;// 照相机参数集
@Override
public void onCreate(Bundle savedInstanceState) {
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// SD卡允许读写
Toast.makeText(PhotographActivity.this, "SD卡没有插入或不能读写",
Toast.LENGTH_LONG).show();
}
Window window = getWindow();
// 只要这个窗口是用户可见的,保持设备的屏幕打开及明亮。
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// 选择支持半透明模式,在有surfaceview的activity中使用。
window.setFormat(PixelFormat.TRANSLUCENT);
BaseActivity.isFullSreen = true;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photograph);
init();
}
@SuppressLint("NewApi")
private void init() {
takePic = (ImageView) findViewById(R.id.btn_picture);// 拍照
back = (ImageView) findViewById(R.id.btn_backcapter);// 返回
flash = (ImageView) findViewById(R.id.btn_flash);// 调整闪光效果
zoomSet = (SeekBar) findViewById(R.id.seekbar_focal);// 调整焦距
cameraBG = (SurfaceView) findViewById(R.id.camrea);// 相机预览
// 固定surfaceView的高宽
cameraBG.getHolder().setFixedSize((int) BaseActivity.ScreenW,
(int) BaseActivity.ScreenH);
// 设置该surfaceView不用自己维护缓冲区
cameraBG.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 设置surfaceView的回调函数
cameraBG.getHolder().addCallback(new SurfaceCallback());
zoomSet.setOnSeekBarChangeListener(this);
takePic.setOnClickListener(this);
back.setOnClickListener(this);
flash.setOnClickListener(this);
cameraBG.setOnClickListener(this);
}
private final class SurfaceCallback implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (camera != null) {
camera.release();// 相机释放
}
camera = Camera.open();// 打开相机
camera.setDisplayOrientation(90);// 旋转镜头
parameters = camera.getParameters();// 获取相机参数集
if (parameters.getFlashMode() == null) {
flash.setImageBitmap(BitmapFactory.decodeResource(
getResources(), flashImg[FLASH_OFF]));
Toast.makeText(PhotographActivity.this, "无闪光灯",
Toast.LENGTH_SHORT).show();
} else {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
parameters.setPreviewSize(100, 120);// 设置预览照片的大小
parameters.setPictureFormat(ImageFormat.JPEG);//设置图片格式
parameters.setPictureSize(100, 120);// 设置照片的大小
camera.setParameters(parameters);
}
maxZoom = parameters.getMaxZoom();// 获取最大像素
zoomSet.setMax(maxZoom);// 设置最大像素
try {
camera.setPreviewDisplay(cameraBG.getHolder());// 设置相机预览
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();// 开始预览
preview = true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
if (preview) {
camera.stopPreview();
}
camera.release();
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (camera != null && event.getRepeatCount() == 0) {
switch (keyCode) {
case KeyEvent.KEYCODE_CAMERA:
case KeyEvent.KEYCODE_DPAD_CENTER:
takPicture();
break;
case KeyEvent.KEYCODE_VOLUME_UP:
zoom = (zoom + 5) < maxZoom ? (zoom + 5) : maxZoom;
zoomSet.setProgress(zoom);
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
zoom = (zoom - 5) > 0 ? (zoom - 5) : 0;