无法使用相机套件拍照



我正在关注这个文档 http://docs.camerakit.website/#/我正在尝试拍照,但找不到确切的代码。 当我在我的onCreate方法中编写此代码时,android工作室对我说这不是一个正确的代码:

camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
// Create a bitmap    
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
});
camera.captureImage();

这是我的整个代码:

public class MainActivity extends AppCompatActivity {
CameraView cameraView;
ImageView img_photo;
Bitmap photo;
Button btt_scatta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Get root view from Activity
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
cameraView = (CameraView) findViewById(R.id.camera);
cameraView.setFacing(CameraKit.Constants.FACING_FRONT);
btt_scatta = (Button) findViewById(R.id.btt_scatta);
img_photo = (ImageView) findViewById(R.id.img_photo);
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
// Create a bitmap    
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
});
btt_scatta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
photo = getScreenShot(rootView);
img_photo.setImageBitmap(photo);
}
});

}
@Override
protected void onResume() {
super.onResume();
cameraView.start();
}
@Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
//Capture the root view
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
//Store the Bitmap into the phone
public static void store(Bitmap bm, String fileName){
final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dirPath);
if(!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

我已经遵循了整个代码,没有任何结果。您可以在github网站上找到代码:https://github.com/CameraKit/camerakit-android 我的目标是创建一个应用程序,从您的相机拍摄照片,然后在 Imageview 中向您展示图片。 我想说相机使用此代码工作正常。我对相机没有任何问题,但只是在拍照的那一刻。 谢谢大家。

你应该调用

camera.captureImage()

在btt_scatta的点击侦听器内。

最新更新