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;
zoomSet.setProgress(zoom);
break;
case KeyEvent.KEYCODE_BACK:
if (flashOpen) {
popupWindow.dismiss();
} else {
finish();
}
}
}
return true;
}
private final class TakePictureCallback implements PictureCallback {
@Override
public void onPictureTaken(byte[] data, Camera camera) { }
}
public void takPicture() {
camera.autoFocus(null);// 自动对焦
camera.takePicture(null, null, null, new TakePictureCallback());
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
zoom = zoomSet.getProgress();
parameters.setZoom(zoom);
camera.setParameters(parameters);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_picture:
takPicture();
break;
case R.id.btn_backcapter:
if (flashOpen) {
popupWindow.dismiss();
} else {
finish();
}
break;
case R.id.btn_flash:
if (parameters.getFlashMode() == null) {
Toast.makeText(PhotographActivity.this, “无闪光灯”,
Toast.LENGTH_SHORT).show();
} else if (parameters.getFlashMode() == Camera.Parameters.FLASH_MODE_AUTO) {
flash.setImageBitmap(BitmapFactory.decodeResource(
getResources(), flashImg[FLASH_OFF]));
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
} else if (parameters.getFlashMode() == Camera.Parameters.FLASH_MODE_OFF) {
flash.setImageBitmap(BitmapFactory.decodeResource(
getResources(), flashImg[FLASH_ON]));
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
camera.setParameters(parameters);
} else if (parameters.getFlashMode() == Camera.Parameters.FLASH_MODE_ON) {
flash.setImageBitmap(BitmapFactory.decodeResource(
getResources(), flashImg[FLASH_AUTO]));
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
camera.setParameters(parameters);
}
break;
case R.id.camrea:
camera.autoFocus(null);
break;
}
}
@Override
protected void onPause() {
super.onPause();
zoomSet.setProgress(0);// 让SeekBar条初始化
}
public void run() {
while (true) {
if (camera != null) {
camera.autoFocus(null);
}
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
程序中一旦屏蔽红字部分的代码 parameters.setPreviewSize(100, 120);// 设置预览照片的大小
parameters.setPictureFormat(ImageFormat.JPEG);//设置图片格式
parameters.setPictureSize(100, 120);// 设置照片的大小
就能正常的实现手机照相功能了
如果不屏蔽[ color=#FF0000] parameters.setPreviewSize(100, 120);// 设置预览照片的大小
parameters.setPictureFormat(ImageFormat.JPEG);//设置图片格式
parameters.setPictureSize(100, 120);// 设置照片的大小
[/color
Log日志则会报如下错误:
09-24 16:58:52.385: E/AndroidRuntime(25089): FATAL EXCEPTION: main
09-24 16:58:52.385: E/AndroidRuntime(25089): java.lang.RuntimeException: setParameters failed
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.hardware.Camera.native_setParameters(Native Method)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.hardware.Camera.setParameters(Camera.java:1449)
09-24 16:58:52.385: E/AndroidRuntime(25089): at app.example.takephoto.PhotographActivity$SurfaceCallback.surfaceCreated(PhotographActivity.java:117)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.view.SurfaceView.access$000(SurfaceView.java:81)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1892)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1070)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4296)
09-24 16:58:52.385: E/AndroidRuntime(25089): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
也就是说上述parameters调用的三个方法的任意一个不屏蔽都会报错,小弟实在找不出解决方法。跪求大神指导