【问题标题】:Android Camera ExampleAndroid 相机示例
【发布时间】:2015-10-03 10:26:23
【问题描述】:

我正在开发android相机应用程序,我找到了一个代码:

http://examples.javacodegeeks.com/android/core/hardware/camera-hardware/android-camera-example/

但我遇到了两个我无法理解的问题。第一个在这部分:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    myContext = this;
    initialize();
}

其实我没看懂什么是initialize(),它的作用是什么?我搜索了它,但我没有找到任何东西!

第二个问题在这部分:

public void onResume() {
    super.onResume();

    if (!hasCamera(myContext)) {

        Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
        toast.show();
        finish();
    }
    if (mCamera == null) {

        //if the front facing camera does not exist

        if (findFrontFacingCamera()  1) {

            //release the old camera instance

            //switch camera, from the front and the back and vice versa

            releaseCamera();
            chooseCamera();

        } else {
            Toast toast = Toast.makeText(myContext, "Sorry, your phone has only one camera!", Toast.LENGTH_LONG);
            toast.show();
        }
    }
};

我没看懂这个表达式:if (findFrontFacingCamera() 1). 因为findFrontFacingCamera() 不是boolean 并且1 有问题!

【问题讨论】:

    标签: java android camera


    【解决方案1】:

    教程中有错别字和遗漏。每个下载项目的 onResume 是

    super.onResume();
        if (!hasCamera(myContext)) {
            Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
            toast.show();
            finish();
        }
        if (mCamera == null) {
            if (findFrontFacingCamera() < 0) {
                Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
                switchCamera.setVisibility(View.GONE);
            }
            mCamera = Camera.open(findBackFacingCamera());
            mPicture = getPictureCallback();
            mPreview.refreshCamera(mCamera);
        }
    

    顺便说一句,教程中也不包含初始化方法。代码如下:

    public void initialize() {
        cameraPreview = (LinearLayout) findViewById(R.id.camera_preview);
        mPreview = new CameraPreview(myContext, mCamera);
        cameraPreview.addView(mPreview);
    
        capture = (Button) findViewById(R.id.button_capture);
        capture.setOnClickListener(captureListener);
    
        switchCamera = (Button) findViewById(R.id.button_ChangeCamera);
        switchCamera.setOnClickListener(switchCameraListener);
    }
    

    【讨论】:

      【解决方案2】:
      initialize()
      

      确保您的相机已初始化

      不明白这个表达式:if (findFrontFacingCamera() 1)。 因为 findFrontFacingCamera() 不是布尔值并且存在问题 1!

      在这种情况下,当您尝试查找摄像头时(通常设备上有 2 个摄像头,但在某些情况下只有后置摄像头或前置摄像头) 所以“1”标志表示您要选择第二个摄像头,而“0”将选择第一个(通常是后置)摄像头

      【讨论】:

      • 但是当我写initialize()时,eclipse说它是未定义的并给出错误!
      【解决方案3】:

      我相信这是一个错字??来自评论行,

      //if the front facing camera does not exist 
      

      在 if 条件之前,我可以看到需要进行以下更改。 换行

      if (findFrontFacingCamera() 1) 
      

      if (findFrontFacingCamera()== -1) 
      

      正如我在示例中看到的,这是一个返回相机 id 的方法。

      请注意,对于前置摄像头 id = 1,对于后置摄像头,它的值为 0。

      【讨论】:

        猜你喜欢
        • 2012-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多