谷歌移动视觉低图像质量

时间:2018-04-10 08:50:39

标签: java android bitmap android-image-capture

我尝试从带有移动视觉库的相机拍照,但拍摄的图像质量很低(分辨率240x320)

我不知道应该在哪里修改此功能(我猜它与CameraSource有关),我不确定是否应该修改。

这是我的捕获功能(从这里找到:How Capture Picture while mobile vision api - face tracking

public void takePicture(){

        cameraSource.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(0);
                    rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
                            loadedImage.getWidth(), loadedImage.getHeight(),
                            rotateMatrix, false);

                    File 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");

                        pictureName = imageFile.getName();
                        Log.i("Picture Name ", pictureName);
                        imageFile.createNewFile();

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

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

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

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

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

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

这是CameraSource的创建:

        cameraSource = new CameraSource.Builder(getApplicationContext(), detector)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1280, 1024)
                .setRequestedFps(2.0f)
                .setAutoFocusEnabled(true)
                .build();

我应该修改任何内容还是在这里添加内容?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我通过更改这两行解决了这个问题:

sudo npm install

我将格式更改为PNG

 rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);

在创建变量

时更改了cameraource的这些属性
rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
相关问题