【发布时间】:2016-05-12 06:09:31
【问题描述】:
我正在学习 Android Camera Api,我正在 Nexus 6 设备上对其进行测试。我下载了一个在设备上运行良好的 Camera Api 教程。然后我创建了自己的项目并基本上复制了代码,但每当我调用 Camera.open() 时,该代码总是返回 null。代码几乎相同。
清单也是相同的,我的 gradle 文件也是如此。
但是我从网站下载的教程有效,而我从 Android Studio 创建的教程无效。
我无法弄清楚我做错了什么。我已经检查了有关此问题的先前问题,但我没有犯同样的错误。清单权限位于正确的位置(在应用程序标签上方),Nexus 6 有一个后置摄像头,因此没有摄像头不是问题。
非工作清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gr3ymatter.cameraapidemo">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
</manifest>
非工作 MainActivity:
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private ImageSurfaceView mImageSurfaceView;
private Camera camera;
private FrameLayout cameraPreviewLayout;
private ImageView capturedImageHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
cameraPreviewLayout = (FrameLayout)findViewById(R.id.camera_preview);
capturedImageHolder = (ImageView)findViewById(R.id.captured_image);
camera = checkDeviceCamera();
mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera);
cameraPreviewLayout.addView(mImageSurfaceView);
Button captureButton = (Button)findViewById(R.id.button);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
camera.takePicture(null, null, pictureCallback);
}
});
}
private Camera checkDeviceCamera(){
Camera mCamera = null;
try {
mCamera = Camera.open();
} catch (Exception e) {
e.printStackTrace();
}
return mCamera;
}
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if(bitmap==null){
Toast.makeText(MainActivity.this, "Captured image is empty", Toast.LENGTH_LONG).show();
return;
}
capturedImageHolder.setImageBitmap(scaleDownBitmapImage(bitmap, 300, 200 ));
}
};
private Bitmap scaleDownBitmapImage(Bitmap bitmap, int newWidth, int newHeight){
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
return resizedBitmap;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
非工作 SurfaceView 类
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
/**
* Created by Afzal on 2/2/16.
*/
public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private Camera camera;
private SurfaceHolder surfaceHolder;
public ImageSurfaceView(Context context, Camera camera) {
super(context);
this.camera = camera;
this.surfaceHolder = getHolder();
this.surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
this.camera.setPreviewDisplay(holder);
this.camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
this.camera.stopPreview();
this.camera.release();
}
}
Working Projects 文件与我在遵循示例时从字面上复制的文件相同。如果有帮助,我可以将 github 链接粘贴到工作项目和非工作项目?
编辑:这是两个项目的回购 https://github.com/Gr3ymatter/CameraApiDemo_Working https://github.com/Gr3ymatter/CameraApiDemo_NotWorking
编辑:LOGCAT 输出。同样基于 Madhurs 有用的 cmets,我认为这可能是一个设备问题,所以我在 Nexus 7 上运行了它,但我仍然遇到了同样的问题。
提前感谢您的帮助!
【问题讨论】:
标签: android android-camera android-permissions