我是Android编程新手。我正在开发一个名为放大镜的简单应用程序,它将使用相机镜头放大和缩小预览。但是由于放大和缩小都不起作用,我对我的代码感到困惑。下面是我的预览课程,当我在手机上运行应用程序时,没有错误,但放大和缩小都无法正常工作。有人请帮我修复此错误
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
int currentZoomLevel = 0;
int maxZoomLevel = 0;
int MAX_ZOOM = 4;
Button buttonzoomin;
Button buttonzoomout;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPreviewFrame - wrote bytes: "
+ data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
//camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
final Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
buttonzoomin = (Button) findViewById(R.id.buttonzoomin);
buttonzoomout = (Button) findViewById(R.id.buttonzoomout);
// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize = previewSizes.get(0);
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
parameters.setPreviewSize(previewSize.height, previewSize.width);
camera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
parameters.setPreviewSize(previewSize.width, previewSize.height);
}
if(display.getRotation() == Surface.ROTATION_180)
{
parameters.setPreviewSize(previewSize.height, previewSize.width);
}
if(display.getRotation() == Surface.ROTATION_270)
{
parameters.setPreviewSize(previewSize.width, previewSize.height);
camera.setDisplayOrientation(180);
}
if(parameters.isSmoothZoomSupported()){
buttonzoomin.setEnabled(true);
buttonzoomout.setEnabled(true);
maxZoomLevel = parameters.getMaxZoom();
buttonzoomin.setOnClickListener(new OnClickListener(){
public void onClick(View v){
currentZoomLevel=currentZoomLevel + 1;
if(currentZoomLevel > maxZoomLevel)
{
currentZoomLevel = maxZoomLevel;;
}
//camera.startSmoothZoom(currentZoomLevel);
parameters.setZoom(currentZoomLevel);
camera.stopPreview();
camera.startPreview();
camera.setParameters(parameters);
}
});
buttonzoomout.setOnClickListener(new OnClickListener(){
public void onClick(View v){
currentZoomLevel = currentZoomLevel-1;
if(currentZoomLevel < 0)
{
currentZoomLevel = 0;
}
//camera.startSmoothZoom(currentZoomLevel);
parameters.setZoom(currentZoomLevel);
camera.stopPreview();
camera.startPreview();
camera.setParameters(parameters);
}
});
}
camera.setParameters(parameters);
camera.startPreview(); }
@Override
public void draw(Canvas canvas)
{
super.draw(canvas);
Paint p = new Paint(Color.RED);
Log.d(TAG, "draw");
canvas.drawText("PREVIEW", canvas.getWidth() / 2,
canvas.getHeight() / 2, p);
} }
答案 0 :(得分:0)
尝试不使用平滑缩放。许多设备不一定支持平滑缩放,目前您没有支持平滑缩放的备份情况 - 例如,您的放大按钮永远不会启用!