如何在移动视觉api时捕获图片-人脸跟踪 [英] How Capture Picture while mobile vision api - face tracking

查看:838
本文介绍了如何在移动视觉api时捕获图片-人脸跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mobile vision api的面部跟踪示例,并且试图通过在屏幕上点击来拍照.首先,我想用按钮拍摄屏幕上的任何图片,然后尝试使用此代码,但失败了.我看一下条形码读取器示例,有点击方法,但我无法成功.我应该在这种情况下使用哪种方法?

I'm using the Mobile vision api's face tracking example and i'm trying to take picture with tapping on the screen. Firstly i wanted to take any picture on the screen with button and i tryed this code but it failed. I look at the barcode reader example and there is tap method but i couldn't succeed. What approach that i should use this case?

纯FaceTracking github代码

private void takeImage() {
    camera.takePicture(null, null, new PictureCallback() {

        private File imageFile;

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

                // rotate Image
                Matrix rotateMatrix = new Matrix();
                rotateMatrix.postRotate(rotation);
                rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
                        loadedImage.getWidth(), loadedImage.getHeight(),
                        rotateMatrix, false);
                String state = Environment.getExternalStorageState();
                File folder = null;
                if (state.contains(Environment.MEDIA_MOUNTED)) {
                    folder = new File(Environment
                            .getExternalStorageDirectory() + "/Demo");
                } else {
                    folder = new File(Environment
                            .getExternalStorageDirectory() + "/Demo");
                }

                boolean success = true;
                if (!folder.exists()) {
                    success = folder.mkdirs();
                }
                if (success) {
                    java.util.Date date = new java.util.Date();
                    imageFile = new File(folder.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());

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

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
}

@Override
public void onClick(View v) {
    case R.id.captureImage:
        takeImage();
        break;
    default:
        break;
    }
}

推荐答案

我解决了这个问题.

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();
                    }
                }
            });
        }
    });

这篇关于如何在移动视觉api时捕获图片-人脸跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆