当我添加缩放代码时,我的代码工作正常,它给出了空指针异常。 我在预览类中编写了以下代码。我是android新手,请提前帮助谢谢。
我的代码是
int currentZoomLevel = 0, maxZoomLevel = 0;
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
// start preview with new settings
try {
parameters = mCamera.getParameters();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Display display = ((WindowManager) context
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_0) {
try {
parameters.setPreviewSize(height, width);
mCamera.setDisplayOrientation(90);
} catch (Exception e) {
e.printStackTrace();
}
}
if (display.getRotation() == Surface.ROTATION_90) {
try {
parameters.setPreviewSize(width, height);
} catch (Exception e) {
e.printStackTrace();
}
}
if (display.getRotation() == Surface.ROTATION_180) {
try {
parameters.setPreviewSize(height, width);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (display.getRotation() == Surface.ROTATION_270) {
try {
parameters.setPreviewSize(width, height);
mCamera.setDisplayOrientation(180);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我开始缩放的时候。我在侧面布局中添加缩放控件
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoom);
if (parameters.isZoomSupported()) {
final int maxZoomLevel = parameters.getMaxZoom();
Log.i("max ZOOM ", "is " + maxZoomLevel);
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel < maxZoomLevel) {
currentZoomLevel++;
mCamera.startSmoothZoom(currentZoomLevel);
parameters.setZoom(currentZoomLevel);
mCamera.setParameters(parameters);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
if (currentZoomLevel > 0) {
currentZoomLevel--;
parameters.setZoom(currentZoomLevel);
mCamera.setParameters(parameters);
}
}
});
} else
zoomControls.setVisibility(View.GONE);
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (Exception e) {
// intentionally left blank for a test
}
}