相机API不工作的奇巧奇巧、相机、工作、API

2023-09-06 03:10:38 作者:怣

我有一个很奇怪的问题。下面code我来承担按钮,单击图片。它的工作原理上正确果冻豆的手机,但不是在奇巧:

I have a really strange problem. The following code I have is used to take a picture on button click. It works properly on Jelly Bean phones, but not on Kitkat:

MainActivity.java

package com.example.takepic;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {
  private final static String DEBUG_TAG = "MakePhotoActivity";
  private Camera camera;
  private Button capture = null;
  private int cameraId = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  capture = (Button)findViewById(R.id.captureBack);
  capture.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         camera.startPreview(); //After this, nothing gets printed, and picture does not get taken
        System.out.println("Camera preview has started.");
            camera.takePicture(null, null, new PhotoHandler(getApplicationContext()));
    }
});
// do we have a camera?
if (!getPackageManager()
    .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
  Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
      .show();
} else {
  cameraId = findBackFacingCamera();
  if (cameraId < 0) {
    Toast.makeText(this, "No back facing camera found.",
        Toast.LENGTH_LONG).show();
  } else {
    camera = Camera.open(cameraId);
  }
}
  }



 private int findBackFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
  CameraInfo info = new CameraInfo();
  Camera.getCameraInfo(i, info);
  if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
    Log.d(DEBUG_TAG, "Camera found");
    cameraId = i;
    break;
  }
}
return cameraId;
}

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

} 

PhotoHandler.java

package com.example.takepic;



import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class PhotoHandler implements PictureCallback {

  private final Context context;

  public PhotoHandler(Context context) {
    this.context = context;
  }

  @Override
 public void onPictureTaken(byte[] data, Camera camera) {

   File pictureFileDir = getDir();
  Toast.makeText(context, "Entered onPictureTaken", Toast.LENGTH_LONG).show();
   if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

  Log.d("Directory error", "Can't create directory to save image.");
  Toast.makeText(context, "Can't create directory to save image.",
      Toast.LENGTH_LONG).show();
  return;

 }

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";

String filename = pictureFileDir.getPath() + File.separator + photoFile;

File pictureFile = new File(filename);

try {
  FileOutputStream fos = new FileOutputStream(pictureFile);
  fos.write(data);
  fos.close();
  Toast.makeText(context, "New Image saved:" + photoFile,
      Toast.LENGTH_LONG).show();
} catch (Exception error) {
  Log.d("File saving error", "File" + filename + "not saved: "
      + error.getMessage());
  Toast.makeText(context, "Image could not be saved.",
      Toast.LENGTH_LONG).show();
  }
}

  private File getDir() {
  //  File sdDir =    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
 File sdDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
 Toast.makeText(context, ("Path : "+sdDir.getAbsolutePath()), Toast.LENGTH_LONG).show();
  return sdDir;
 }
} 

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.takepic"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.takepic.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我已经把不少敬酒消息,并打印报表用于调试的目的。我还没有张贴的logcat在这里,因为,当我运行这个在奇巧的电话,我什么也得不到的logcat的。没有异常或警告。

I have put quite a few toast messages and print statements for debugging purposes. I have not posted logcat here, because, when I run this on a KitKat phone, I get nothing on logcat. No exception or warning.

当我运行这个在软糖手机,它运行正常,显示所有的祝酒词,并打印并拍摄照片。

When I run this on a jellybean phone, it runs properly, displaying all the toasts and prints and takes the picture.

当我运行这个在奇巧,我没有得到任何经过调试消息

When I run this on Kitkat, I do not get any debugging messages after the

camera.startPreview();
System.out.println("Camera preview has started.");

我怀疑问题出在takePicture API,但我无法调试。

I suspect problem is with the takePicture API, but I am unable to debug it.

请帮我解决这个问题。

编辑:进一步分析后,我找到了问题的原因。在 PhotoHandler 对象被调用成功,但的 onPictureTaken 的方法不叫,可能是因为它没有得到信息的图片被点击相机。我不知道为什么。请帮我解决这个问题。

After further analysis, I found the reason for issue. The PhotoHandler object gets invoked successfully, but the onPictureTaken method is not called, probably because it did not get information that picture was clicked by the camera. I don't know why. Please help me fix this issue.

推荐答案

我观察到,你不给予任何表面架相机。给出preVIEW表面到相机是重要的。

I have observed that you are not assigning any surface holder to the camera. Giving a preview surface to the camera is important.

据这里的文档:

http://developer.android.com/guide/topics/media/camera.html

按照code。通过商务部建议。拍摄照片没有preVIEW是一个很大的安全隐患。 Android的球员可能会在奇巧解决了这个问题。

follow the code suggested by the doc. Taking a picture without a preview is a big security concern. Android guys might have fixed this in kitkat.

您可能会错过的code的一部分,而粘贴在这里,所以作为一个额外的关注也检查你执行你的code'camera.takePicture(NULL,NULL,回调)回调方法里面SurfaceHolder的onSurfaceCreated。

You might have missed that part of the code while pasting here so as an added concern also check that you are executing your code 'camera.takePicture(null,null,callback)' inside the callback method 'onSurfaceCreated' of SurfaceHolder.

您可以得到所有相关的code,在上述的链接。

You can get all the relevant code at aforementioned link.