Search code examples
javaandroidrotationandroid-camerasurfaceholder

Android Surface class Surface.ROTATION_ cannot be resolved or is not a field


I'm trying to fix my camera app. When i hold the camera in landscape mode, it previews on the screen sideways. I found this fix on stack : Android - Camera preview is sideways

In my CameraSurfaceView class constructor, I get the surface like this this.Surface = getHolder();. In the surfaceChanged method i check the display rotation against Surface.ROTATION_<#> for 0,90,180,and 270 degrees (method below). However, each one of the ROTATIONSurface constants is underlined with the following error ROTATION_<#> cannot be resolved or is not a field. I am not sure what I am doing wrong. My CameraSurfaceView is separate from my activity so I'm wondering if the display has something to do with it? Hope this is explained correctly, if you need more code or anything let me know. I appreciate this in advance!

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ZoomControls;


public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    int orientation = 0;

    Camera.Parameters params = Cam.getParameters();

    WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
    android.view.Display display = window.getDefaultDisplay();

        if(display.getRotation() == Surface.ROTATION_0)
        {
            params.setPreviewSize(height, width);                           
            Cam.setDisplayOrientation(90);
        }

        if(display.getRotation() == Surface.ROTATION_90)
        {
            params.setPreviewSize(height, width);                           
        }

        if(display.getRotation() == Surface.ROTATION_180)
        {
            params.setPreviewSize(height, width);               
        }

        if(display.getRotation() == Surface.ROTATION_270)
        {
            params.setPreviewSize(height, width);
            Cam.setDisplayOrientation(180);
        }


    List<Camera.Size> previewSizes = params.getSupportedPreviewSizes();
    Camera.Size previewSize = getBestPreviewSize(width, height);
    params.setPreviewSize(previewSize.width, previewSize.height); 



     int zoom = 0;
     params.setZoom(zoom);

    zoomControls.setOnZoomInClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Camera.Parameters params = Cam.getParameters();
                maxZoomLevel = params.getMaxZoom();
                    if(currentZoomLevel < maxZoomLevel){
                        currentZoomLevel++;
                        Cam.startSmoothZoom(currentZoomLevel);
                    }
            }
        });

    zoomControls.setOnZoomOutClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Camera.Parameters params = Cam.getParameters();
                maxZoomLevel = params.getMaxZoom();
                    if(currentZoomLevel > 0){
                        currentZoomLevel--;
                        Cam.startSmoothZoom(currentZoomLevel);
                    }
            }
        });    


    Cam.setParameters(params);

    try {
        Cam.setPreviewDisplay(Surface);
    } catch (IOException e) {
        e.printStackTrace();
    }

    Cam.startPreview();
}

Solution

  • Since you have a field named Surface, you cannot access the class Surface from within your class.

    Instead, you will have to write

    android.view.Surface.ROTATION_0
    

    or rename your field.