web-dev-qa-db-fra.com

android - Impossible de résoudre le problème de la méthode getSystemService

Je rencontre un problème avec la méthode getSystemService, car Android Studio IDE continue de me dire qu'il ne peut tout simplement pas résoudre cette méthode. D'après ce que j'ai lu, cela pourrait venir du fait que ma classe ne contient pas d'activité. Je lisais des rubriques connexes sur le problème getSystemService, mais je ne trouvais aucune solution correspondante.

Je place mon code ci-dessus

import Android.graphics.Bitmap;
import Android.graphics.BitmapFactory;
import Android.graphics.Canvas;
import Android.graphics.Matrix;
import Android.graphics.Rect;
import Android.hardware.Camera;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private Camera mCamera;
private CameraView mCameraView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
protected void onResume(){
    super.onResume();

    try{
        mCamera = Camera.open(1);
        mCameraView = new CameraView(this, mCamera);
        setContentView(mCameraView);
    } catch (Exception e){
        finish();
    }
}

@Override
protected void onPause(){
    if(mCamera != null){
        mCamera.release();
        mCamera = null;
    }
    super.onPause();


   }

}

Le audacieux de la classe ci-dessus est celui que j'essaie de comprendre

import Android.content.Context;
import Android.hardware.Camera;
import Android.hardware.Camera.Parameters;
import Android.util.Log;
import Android.view.SurfaceHolder;
import Android.view.SurfaceView;
import Android.view.View;
import Android.view.Surface;
import Android.view.Display;
import Android.view.WindowManager;
import Java.io.IOException;
import Android.app.Activity;
import Java.util.List;


public class CameraView extends SurfaceView implements SurfaceHolder.Callback{


private Camera mCamera;
private View mView;
private WindowManager display;


public CameraView(Context context, Camera mCamera) {
    super(context);

    this.mCamera = mCamera;
    mCamera.setDisplayOrientation(90);

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_Push_BUFFERS);

}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    try{
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.e("cameraPreview", "The failure of the camera settings");
    }
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters params = mCamera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size optionalSize = getOptimalPreviewSize(sizes, width, height);
    params.setPreviewSize(optionalSize.width, optionalSize.height);
    mCamera.setParameters(params);

    boolean isPreviewRunning = true;

    if (isPreviewRunning)
    {
        mCamera.stopPreview();
    }

    Parameters parameters = mCamera.getParameters();
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();  

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

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

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

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

    mCamera.setParameters(parameters);
    previewCamera();
}

public void previewCamera()
{
    try
    {
        mCamera.setPreviewDisplay();
        mCamera.startPreview();
        boolean isPreviewRunning = true;
    }
    catch(Exception e)
    {
        Log.d(APP_CLASS, "Cannot start preview", e);
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.release();
    mCamera = null;
}

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {

    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio=(double)h / w;

    if (sizes == null) return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;


  }



}

Pourriez-vous éventuellement me dire comment faire pour éviter cela?

Merci!

5
ArturS

Vous devez appeler getSystemService sur une Context. Par conséquent, cela ne fonctionne pas dans votre CameraView. Vous pouvez stocker le contexte que vous avez déjà transmis sous forme de champ et appeler getSystemService à ce sujet. Voir votre code modifié ci-dessous: 

import Android.content.Context;
import Android.hardware.Camera;
import Android.hardware.Camera.Parameters;
import Android.util.Log;
import Android.view.SurfaceHolder;
import Android.view.SurfaceView;
import Android.view.View;
import Android.view.Surface;
import Android.view.Display;
import Android.view.WindowManager;
import Java.io.IOException;
import Android.app.Activity;
import Java.util.List;


public class CameraView extends SurfaceView implements SurfaceHolder.Callback{


private Camera mCamera;
private View mView;
private WindowManager display;
private Context mContext;


public CameraView(Context context, Camera mCamera) {
    super(context);

    mContext = context;

    this.mCamera = mCamera;
    mCamera.setDisplayOrientation(90);

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_Push_BUFFERS);

}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    try{
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.e("cameraPreview", "The failure of the camera settings");
    }
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters params = mCamera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size optionalSize = getOptimalPreviewSize(sizes, width, height);
    params.setPreviewSize(optionalSize.width, optionalSize.height);
    mCamera.setParameters(params);

    boolean isPreviewRunning = true;

    if (isPreviewRunning)
    {
        mCamera.stopPreview();
    }

    Parameters parameters = mCamera.getParameters();
    Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();  

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

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

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

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

    mCamera.setParameters(parameters);
    previewCamera();
}

public void previewCamera()
{
    try
    {
        mCamera.setPreviewDisplay();
        mCamera.startPreview();
        boolean isPreviewRunning = true;
    }
    catch(Exception e)
    {
        Log.d(APP_CLASS, "Cannot start preview", e);
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.release();
    mCamera = null;
}

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {

    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio=(double)h / w;

    if (sizes == null) return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;


  }
10
barq
context!!.getSystemService(Service.WINDOW_SERVICE) as WindowManager;
0
dabary