Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓相机的图像专家_Android_Image_Android Camera - Fatal编程技术网

Android 安卓相机的图像专家

Android 安卓相机的图像专家,android,image,android-camera,Android,Image,Android Camera,一直在尝试将高质量的压缩图像上传到S3,但它一直被删除 这是我的黑客代码在一起已经有几天了,所以会喜欢一些新鲜的眼睛。一直在努力保持每张图片的精确保存大小,并保持纵横比不变 但似乎会影响质量和分辨率 private void saveImage(){ if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) { pictureFile = getOu

一直在尝试将高质量的压缩图像上传到S3,但它一直被删除

这是我的黑客代码在一起已经有几天了,所以会喜欢一些新鲜的眼睛。一直在努力保持每张图片的精确保存大小,并保持纵横比不变

但似乎会影响质量和分辨率

   private void saveImage(){
          if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {
                pictureFile = getOutputMediaFile();
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                int num = Exif.getOrientation(data);
                if (num > 0) {
                    bitmap = rotateImage(bitmap, num);
                }
                try {
                    Bitmap scaledBitmap = scaleImage(bitmap);
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                    Uri contentUri = Uri.fromFile(pictureFile);
                    saveImageLocation(contentUri.getPath());
                    Intent mediaScanIntent = new Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    mediaScanIntent.setData(contentUri);
                    getActivity().sendBroadcast(mediaScanIntent);
                } catch (Exception e) {
                    Toast.makeText(getActivity(), "Could not save your image!",
                            Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getActivity(),
                        "Your phone doesn't have space for image",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

    private Bitmap rotateImage(Bitmap image, int rotate) {
        Toast.makeText(getActivity(), "rotate", Toast.LENGTH_SHORT).show();
        int w = image.getWidth();
        int h = image.getHeight();

        Matrix mtx = new Matrix();
        mtx.preRotate(rotate);
        image = Bitmap.createBitmap(image, 0, 0, w, h, mtx, false);

        return image.copy(Bitmap.Config.ARGB_8888, true);
    }

    private Bitmap scaleImage(Bitmap image) {
        int height = 600;
        double aspectRatio = (double) height / image.getHeight();
        int width = (int) (image.getWidth() * aspectRatio);
        return Bitmap.createScaledBitmap(image, width, height, true);
    }

    private void saveImageLocation(String value) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("image", value);
        editor.commit();
    }

    private boolean isExternalStorageReadOnly() {
        String extStorageState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
            return true;
        }
        return false;
    }

    private boolean isExternalStorageAvailable() {
        String extStorageState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
            return true;
        }
        return false;
    }

    private File getOutputMediaFile() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile = new File(getAlbumStorageDir("companyname") + File.separator
                + "IMG_" + timeStamp + "_" + phoneid + ".jpg");
        return mediaFile;
    }

    private File getAlbumStorageDir(String albumName) {
        File path = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File file = new File(path, albumName);
        file.mkdirs();
        return file;
    }
尝试使用库来调整图像大小和旋转图像

异步任务中
doInBackground写下以下行

Bitmap bitmap = Picasso.with(context).load(file)//
                       .resize(100,100).centerInside().get();
如果图像的方向写入exif数据中,毕加索将自动旋转图像

centerInside()
将确保纵横比保持正确

现在您可以发送图像了