Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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_Android Camera - Fatal编程技术网

android自定义摄像头方向

android自定义摄像头方向,android,android-camera,Android,Android Camera,我已经定义了一个自定义相机视图来拍照。我遇到的问题是,如果照片是用纵向相机拍摄的,图像会旋转90度。我想和Instagram相机一样。 我想要的是,如果我以任何角度旋转相机,我希望位图处于纵向并保存到设备 请帮帮我 这是我的密码: private PictureCallback mPicture = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { saf

我已经定义了一个自定义相机视图来拍照。我遇到的问题是,如果照片是用纵向相机拍摄的,图像会旋转90度。我想和Instagram相机一样。 我想要的是,如果我以任何角度旋转相机,我希望位图处于纵向并保存到设备

请帮帮我

这是我的密码:

 private PictureCallback mPicture = new PictureCallback() {

    public void onPictureTaken(byte[] data, Camera camera) {

        safeToTakePicture = true;

        File pictureFile = new File(Common.FILE_IMAGE_STORAGE, filename);

        if (pictureFile.exists()) {
            pictureFile.delete();
        }

        try {
            FileOutputStream purge = new FileOutputStream(pictureFile);
            purge.write(data);
            purge.close();
        } catch (FileNotFoundException e) {
                } 
          catch (IOException e) {
        }

        if (data != null) {

            new AsyncBitmapCreation(filepath).execute();
        }
    }
};

 class AsyncBitmapCreation extends AsyncTask<Void, Void, Bitmap> {

    byte[] data;

    public AsyncBitmapCreation(byte[] data) {
        this.data = data;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {

        return rotateImage(data);
    }

    @Override
    protected void onPostExecute(Bitmap result) {

        bitmap = result;

    }

}
在旋转函数中,我再次使用ExiFinInterface

 private Bitmap rotateImage(final byte[] data) {
    Bitmap scaledBitmap = null;

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 3;

        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length,
                options);

        int actualHeight = bmp.getHeight();
        int actualWidth = bmp.getWidth();

        float maxHeight = 1020.0f;
        float maxWidth = 680.0f;
        float imgRatio = actualWidth / actualHeight;
        float maxRatio = maxWidth / maxHeight;

        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;

            }
        }

        options.inSampleSize = calculateInSampleSize(options, actualWidth,
                actualHeight);

        // inJustDecodeBounds set to false to load the actual bitmap
        options.inJustDecodeBounds = false;

        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];

        try {

            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight,
                    conf);

        } catch (OutOfMemoryError exception) {

        }

        float ratioX = actualWidth / (float) options.outWidth;
        float ratioY = actualHeight / (float) options.outHeight;
        float middleX = actualWidth / 2.0f;
        float middleY = actualHeight / 2.0f;

        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);

        canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2,
                middleY - bmp.getHeight() / 2, new Paint(
                        Paint.FILTER_BITMAP_FLAG));

        ExifInterface exif;
        try {

            exif = new ExifInterface(fileUri.getPath());
            Matrix matrix = new Matrix();
            if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("6")) {
                matrix.postRotate(90);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("8")) {
                matrix.postRotate(270);

            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("3")) {
                matrix.postRotate(180);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("0")) {
                matrix.postRotate(90);

            }

            scaledBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                    bmp.getHeight(), matrix, true);

        } catch (IOException e) {

        }

    } catch (Exception e) {

    }

    return scaledBitmap;

}
Surfaceview和callback引用此链接


您好,请使用此代码,它将帮助您。。。如果你需要帮助,告诉我

 public void takePhoto() {

            Intent intent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            File folder = new File(Environment.getExternalStorageDirectory() + "/"
                    + getResources().getString(R.string.app_name));

            if (!folder.exists()) {
                folder.mkdir();
            }
            final Calendar c = Calendar.getInstance();
            String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
                    + ((c.get(Calendar.MONTH)) + 1) + "-" + c.get(Calendar.YEAR)
                    + " " + c.get(Calendar.HOUR) + "-" + c.get(Calendar.MINUTE)
                    + "-" + c.get(Calendar.SECOND);
            imageUrl = String.format(
                    Environment.getExternalStorageDirectory() + "/"
                            + getResources().getString(R.string.app_name)
                            + "/%s.png", "Accident_Info(" + new_Date + ")");
            File photo = new File(imageUrl);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("exit", "false");
            intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

            AccidentInfo.this.startActivityForResult(intent, CAMERA_PIC_REQUEST);
        }


private Bitmap callTempMethod(String tempPath) {
        if (tempPath != null) {
            Uri selectedImage = Uri.parse(tempPath);
            if (!TextUtils.isEmpty(tempPath)) {
                LogMessage.i(TAG, "LLLpath::" + tempPath);
                File file = new File(tempPath);
                if (selectedImage.toString().startsWith("content")) {
                    return DealershipApplication.decodeStrem(file,
                            selectedImage, mContext);
                } else {
                    if (!file.isDirectory()) {
                        return DealershipApplication.decodeFile(file, 10, 0);
                    } else {
                        return null;
                    }
                }
            } else
                return null;
        } else {
            return null;
        }
    }


public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }


public static Bitmap getSampleBitmapFromFile(String bitmapFilePath,
            int reqWidth, int reqHeight) throws FileNotFoundException {
        // calculating image size
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                new FileInputStream(new File(bitmapFilePath)), null, options);

        int scale = calculateInSampleSize(options, reqWidth, reqHeight);

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;

        return BitmapFactory.decodeStream(new FileInputStream(new File(
                bitmapFilePath)), null, o2);
    }

当你拍摄照片时,它会以爱国者模式保存。请根据您的需要更改此代码。谢谢。

如果此代码不适用,请告诉我。我会在其他方面帮助你。你能给我推荐一些surfaceview的例子吗?现在我已经更新了所有的课程。看看。。。根据您的需要使用代码。我不是在使用Intent,而是在使用surfaceview和camerapreview
 public void takePhoto() {

            Intent intent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            File folder = new File(Environment.getExternalStorageDirectory() + "/"
                    + getResources().getString(R.string.app_name));

            if (!folder.exists()) {
                folder.mkdir();
            }
            final Calendar c = Calendar.getInstance();
            String new_Date = c.get(Calendar.DAY_OF_MONTH) + "-"
                    + ((c.get(Calendar.MONTH)) + 1) + "-" + c.get(Calendar.YEAR)
                    + " " + c.get(Calendar.HOUR) + "-" + c.get(Calendar.MINUTE)
                    + "-" + c.get(Calendar.SECOND);
            imageUrl = String.format(
                    Environment.getExternalStorageDirectory() + "/"
                            + getResources().getString(R.string.app_name)
                            + "/%s.png", "Accident_Info(" + new_Date + ")");
            File photo = new File(imageUrl);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("exit", "false");
            intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

            AccidentInfo.this.startActivityForResult(intent, CAMERA_PIC_REQUEST);
        }


private Bitmap callTempMethod(String tempPath) {
        if (tempPath != null) {
            Uri selectedImage = Uri.parse(tempPath);
            if (!TextUtils.isEmpty(tempPath)) {
                LogMessage.i(TAG, "LLLpath::" + tempPath);
                File file = new File(tempPath);
                if (selectedImage.toString().startsWith("content")) {
                    return DealershipApplication.decodeStrem(file,
                            selectedImage, mContext);
                } else {
                    if (!file.isDirectory()) {
                        return DealershipApplication.decodeFile(file, 10, 0);
                    } else {
                        return null;
                    }
                }
            } else
                return null;
        } else {
            return null;
        }
    }


public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }


public static Bitmap getSampleBitmapFromFile(String bitmapFilePath,
            int reqWidth, int reqHeight) throws FileNotFoundException {
        // calculating image size
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                new FileInputStream(new File(bitmapFilePath)), null, options);

        int scale = calculateInSampleSize(options, reqWidth, reqHeight);

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;

        return BitmapFactory.decodeStream(new FileInputStream(new File(
                bitmapFilePath)), null, o2);
    }