代码之家  ›  专栏  ›  技术社区  ›  Twing90

无法使用camerakit拍照

  •  0
  • Twing90  · 技术社区  · 6 年前

    http://docs.camerakit.website/#/ 我想拍张照片,但找不到确切的密码。

    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 我想说的是,相机可以很好地使用这个代码。我对相机没有任何问题,只是在拍照的那一刻。 谢谢大家。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Pietro Scarampella    6 年前

    camera.captureImage()