How Capture Picture while mobile vision api - face tracking

▼魔方 西西 提交于 2019-12-01 08:18:43

I solved the problem.

findViewById(R.id.capture).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
                private File imageFile;

                @Override
                public void onPictureTaken(byte[] bytes) {
                    try {
                        // convert byte array into bitmap
                        Bitmap loadedImage = null;
                        Bitmap rotatedBitmap = null;
                        loadedImage = BitmapFactory.decodeByteArray(bytes, 0,
                                bytes.length);

                        Matrix rotateMatrix = new Matrix();
                        rotateMatrix.postRotate(rotation);
                        rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
                                loadedImage.getWidth(), loadedImage.getHeight(),
                                rotateMatrix, false);

                        dir = new File(
                                Environment.getExternalStoragePublicDirectory(
                                        Environment.DIRECTORY_PICTURES), "MyPhotos");

                        boolean success = true;
                        if (!dir.exists())
                        {
                            success = dir.mkdirs();
                        }
                        if (success) {
                            java.util.Date date = new java.util.Date();
                            imageFile = new File(dir.getAbsolutePath()
                                    + File.separator
                                    + new Timestamp(date.getTime()).toString()
                                    + "Image.jpg");

                            imageFile.createNewFile();
                        } else {
                            Toast.makeText(getBaseContext(), "Image Not saved",
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        ByteArrayOutputStream ostream = new ByteArrayOutputStream();

                        // save image into gallery
                        rotatedBitmap.compress(CompressFormat.JPEG, 100, ostream);

                        FileOutputStream fout = new FileOutputStream(imageFile);
                        fout.write(ostream.toByteArray());
                        fout.close();
                        ContentValues values = new ContentValues();

                        values.put(Images.Media.DATE_TAKEN,
                                System.currentTimeMillis());
                        values.put(Images.Media.MIME_TYPE, "image/jpeg");
                        values.put(MediaStore.MediaColumns.DATA,
                                imageFile.getAbsolutePath());

                        FaceTrackerActivity.this.getContentResolver().insert(
                                Images.Media.EXTERNAL_CONTENT_URI, values);

                        //saveToInternalStorage(loadedImage);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });

Less lines of code

                    @Override
                    public void onPictureTaken(byte[] bytes) {
                        Log.d(TAG, "onPictureTaken - jpeg");
                        capturePic(bytes);
                    }

                    private void capturePic(byte[] bytes) {
                        try {
                            String mainpath = getExternalStorageDirectory() + separator + "MaskIt" + separator + "images" + separator;
                            File basePath = new File(mainpath);
                            if (!basePath.exists())
                                Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success": "Failed");
                            File captureFile = new File(mainpath + "photo_" + getPhotoTime() + ".jpg");
                            if (!captureFile.exists())
                                Log.d("CAPTURE_FILE_PATH", captureFile.createNewFile() ? "Success": "Failed");
                            FileOutputStream stream = new FileOutputStream(captureFile);
                            stream.write(bytes);
                            stream.flush();
                            stream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    private String getPhotoTime(){
                        SimpleDateFormat sdf=new SimpleDateFormat("ddMMyy_hhmmss");
                        return sdf.format(new Date());
                    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!