无法使用camerakit拍照

时间:2018-10-05 13:35:41

标签: android camera photo

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

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中向您显示照片。 我想说的是,此代码可使相机正常工作。我的相机没有问题,只是在拍照的时候。 谢谢大家。

1 个答案:

答案 0 :(得分:1)

您应该致电

camera.captureImage()

在btt_scatta的点击侦听器内部。