Android:在横向模式下强制拍摄的照片

Android:在横向模式下强制拍摄的照片,android,image,camera,photo,Android,Image,Camera,Photo,嗨,我已经建立了 最初它甚至在纵向模式下也显示了横向预览。我用一些额外的代码修改了这个。但是,无论您的横向/纵向,它总是以横向模式保存图像 我现在还强制它进入肖像模式: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 基本上,我拍摄的任何图像都是在风景中出现的。(旋转拍摄的肖像图像)。 如何使其在定向模式下保存照片,或锁定为纵向模式 我考虑过拍摄照片阵列并旋转90度。但这需要将图像绘制成位图,旋转,然后存储回

嗨,我已经建立了

最初它甚至在纵向模式下也显示了横向预览。我用一些额外的代码修改了这个。但是,无论您的横向/纵向,它总是以横向模式保存图像

我现在还强制它进入肖像模式:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
基本上,我拍摄的任何图像都是在风景中出现的。(旋转拍摄的肖像图像)。

如何使其在定向模式下保存照片,或锁定为纵向模式


我考虑过拍摄照片阵列并旋转90度。但这需要将图像绘制成位图,旋转,然后存储回数组。杀多了?除非您可以直接旋转图像阵列?

首先使用以下片段检查相机方向:

    private int lookupRotation() {       
       WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
       Display mDisplay = mWindowManager.getDefaultDisplay();
       int rotation = mDisplay.getRotation();
       Log.v(LOG_TAG, "rotation: " + rotation);
       return rotation;
   }
    private Bitmap createBitmap(byte[] imageData, int maxWidth, int maxHeight,
        int rotationDegrees) throws FileNotFoundException {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    options.inDensity = 240;
    int imageWidth = 0;
    int imageHeight = 0;

    Bitmap image = BitmapFactory.decodeByteArray(imageData, 0,
            imageData.length, options);

    imageWidth = image.getWidth();
    imageHeight = image.getHeight();

    if (imageWidth > maxWidth || imageHeight > maxHeight) {

        double imageAspect = (double) imageWidth / imageHeight;
        double desiredAspect = (double) maxWidth / maxHeight;
        double scaleFactor;

        if (imageAspect < desiredAspect) {
            scaleFactor = (double) maxHeight / imageHeight;
        } else {
            scaleFactor = (double) maxWidth / imageWidth;
        }

        float scaleWidth = ((float) scaleFactor) * imageWidth;
        float scaleHeight = ((float) scaleFactor) * imageHeight;

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(image,
                (int) scaleWidth, (int) scaleHeight, true);
        image = scaledBitmap;
    }

    if (rotationDegrees != 0) {

        int w = image.getWidth();
        int h = image.getHeight();
        mtx.postRotate(rotationDegrees);
        Bitmap rotatedBMP = Bitmap.createBitmap(image, 0, 0, w, h, mtx,
                true);
        image = rotatedBMP;
    }

    return image;
}
然后使用检查所需的旋转并设置方向:

if (rotation == Surface.ROTATION_0) {
    int degreesRotate = 90;
}
使用以下代码段调整位图大小并基于方向旋转位图:

    private int lookupRotation() {       
       WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
       Display mDisplay = mWindowManager.getDefaultDisplay();
       int rotation = mDisplay.getRotation();
       Log.v(LOG_TAG, "rotation: " + rotation);
       return rotation;
   }
    private Bitmap createBitmap(byte[] imageData, int maxWidth, int maxHeight,
        int rotationDegrees) throws FileNotFoundException {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    options.inDensity = 240;
    int imageWidth = 0;
    int imageHeight = 0;

    Bitmap image = BitmapFactory.decodeByteArray(imageData, 0,
            imageData.length, options);

    imageWidth = image.getWidth();
    imageHeight = image.getHeight();

    if (imageWidth > maxWidth || imageHeight > maxHeight) {

        double imageAspect = (double) imageWidth / imageHeight;
        double desiredAspect = (double) maxWidth / maxHeight;
        double scaleFactor;

        if (imageAspect < desiredAspect) {
            scaleFactor = (double) maxHeight / imageHeight;
        } else {
            scaleFactor = (double) maxWidth / imageWidth;
        }

        float scaleWidth = ((float) scaleFactor) * imageWidth;
        float scaleHeight = ((float) scaleFactor) * imageHeight;

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(image,
                (int) scaleWidth, (int) scaleHeight, true);
        image = scaledBitmap;
    }

    if (rotationDegrees != 0) {

        int w = image.getWidth();
        int h = image.getHeight();
        mtx.postRotate(rotationDegrees);
        Bitmap rotatedBMP = Bitmap.createBitmap(image, 0, 0, w, h, mtx,
                true);
        image = rotatedBMP;
    }

    return image;
}
private Bitmap createBitmap(字节[]imageData,int-maxWidth,int-maxHeight,
int rotationDegrees)引发FileNotFoundException{
BitmapFactory.Options=new-BitmapFactory.Options();
options.inSampleSize=2;
options.inDensity=240;
int imageWidth=0;
int-imageHeight=0;
位图图像=位图工厂.decodeByteArray(图像数据,0,
imageData.length,选项);
imageWidth=image.getWidth();
imageHeight=image.getHeight();
如果(图像宽度>最大宽度| |图像高度>最大高度){
双图像纵横比=(双)图像宽度/图像高度;
double DesiredSpect=(double)maxWidth/maxHeight;
双尺度因子;
if(图像方面<所需方面){
scaleFactor=(双精度)maxHeight/imageHeight;
}否则{
scaleFactor=(双精度)maxWidth/imageWidth;
}
浮动比例宽度=((浮动)比例因子)*图像宽度;
浮动比例高度=((浮动)比例因子)*图像高度;
位图缩放位图=位图。创建缩放位图(图像,
(int)比例宽度,(int)比例高度,true);
图像=缩放位图;
}
如果(旋转度!=0){
int w=image.getWidth();
int h=image.getHeight();
mtx.后旋转(旋转度);
位图旋转bmp=Bitmap.createBitmap(图像,0,0,w,h,mtx,
正确的);
图像=旋转BMP;
}
返回图像;
}

上述方法将根据方向返回位图。

首先使用以下代码段检查相机方向:

    private int lookupRotation() {       
       WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
       Display mDisplay = mWindowManager.getDefaultDisplay();
       int rotation = mDisplay.getRotation();
       Log.v(LOG_TAG, "rotation: " + rotation);
       return rotation;
   }
    private Bitmap createBitmap(byte[] imageData, int maxWidth, int maxHeight,
        int rotationDegrees) throws FileNotFoundException {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    options.inDensity = 240;
    int imageWidth = 0;
    int imageHeight = 0;

    Bitmap image = BitmapFactory.decodeByteArray(imageData, 0,
            imageData.length, options);

    imageWidth = image.getWidth();
    imageHeight = image.getHeight();

    if (imageWidth > maxWidth || imageHeight > maxHeight) {

        double imageAspect = (double) imageWidth / imageHeight;
        double desiredAspect = (double) maxWidth / maxHeight;
        double scaleFactor;

        if (imageAspect < desiredAspect) {
            scaleFactor = (double) maxHeight / imageHeight;
        } else {
            scaleFactor = (double) maxWidth / imageWidth;
        }

        float scaleWidth = ((float) scaleFactor) * imageWidth;
        float scaleHeight = ((float) scaleFactor) * imageHeight;

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(image,
                (int) scaleWidth, (int) scaleHeight, true);
        image = scaledBitmap;
    }

    if (rotationDegrees != 0) {

        int w = image.getWidth();
        int h = image.getHeight();
        mtx.postRotate(rotationDegrees);
        Bitmap rotatedBMP = Bitmap.createBitmap(image, 0, 0, w, h, mtx,
                true);
        image = rotatedBMP;
    }

    return image;
}
然后使用检查所需的旋转并设置方向:

if (rotation == Surface.ROTATION_0) {
    int degreesRotate = 90;
}
使用以下代码段调整位图大小并基于方向旋转位图:

    private int lookupRotation() {       
       WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
       Display mDisplay = mWindowManager.getDefaultDisplay();
       int rotation = mDisplay.getRotation();
       Log.v(LOG_TAG, "rotation: " + rotation);
       return rotation;
   }
    private Bitmap createBitmap(byte[] imageData, int maxWidth, int maxHeight,
        int rotationDegrees) throws FileNotFoundException {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    options.inDensity = 240;
    int imageWidth = 0;
    int imageHeight = 0;

    Bitmap image = BitmapFactory.decodeByteArray(imageData, 0,
            imageData.length, options);

    imageWidth = image.getWidth();
    imageHeight = image.getHeight();

    if (imageWidth > maxWidth || imageHeight > maxHeight) {

        double imageAspect = (double) imageWidth / imageHeight;
        double desiredAspect = (double) maxWidth / maxHeight;
        double scaleFactor;

        if (imageAspect < desiredAspect) {
            scaleFactor = (double) maxHeight / imageHeight;
        } else {
            scaleFactor = (double) maxWidth / imageWidth;
        }

        float scaleWidth = ((float) scaleFactor) * imageWidth;
        float scaleHeight = ((float) scaleFactor) * imageHeight;

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(image,
                (int) scaleWidth, (int) scaleHeight, true);
        image = scaledBitmap;
    }

    if (rotationDegrees != 0) {

        int w = image.getWidth();
        int h = image.getHeight();
        mtx.postRotate(rotationDegrees);
        Bitmap rotatedBMP = Bitmap.createBitmap(image, 0, 0, w, h, mtx,
                true);
        image = rotatedBMP;
    }

    return image;
}
private Bitmap createBitmap(字节[]imageData,int-maxWidth,int-maxHeight,
int rotationDegrees)引发FileNotFoundException{
BitmapFactory.Options=new-BitmapFactory.Options();
options.inSampleSize=2;
options.inDensity=240;
int imageWidth=0;
int-imageHeight=0;
位图图像=位图工厂.decodeByteArray(图像数据,0,
imageData.length,选项);
imageWidth=image.getWidth();
imageHeight=image.getHeight();
如果(图像宽度>最大宽度| |图像高度>最大高度){
双图像纵横比=(双)图像宽度/图像高度;
double DesiredSpect=(double)maxWidth/maxHeight;
双尺度因子;
if(图像方面<所需方面){
scaleFactor=(双精度)maxHeight/imageHeight;
}否则{
scaleFactor=(双精度)maxWidth/imageWidth;
}
浮动比例宽度=((浮动)比例因子)*图像宽度;
浮动比例高度=((浮动)比例因子)*图像高度;
位图缩放位图=位图。创建缩放位图(图像,
(int)比例宽度,(int)比例高度,true);
图像=缩放位图;
}
如果(旋转度!=0){
int w=image.getWidth();
int h=image.getHeight();
mtx.后旋转(旋转度);
位图旋转bmp=Bitmap.createBitmap(图像,0,0,w,h,mtx,
正确的);
图像=旋转BMP;
}
返回图像;
}

上述方法将根据方向返回位图。

您只需要在横向模式下保存图像,对吗@末日骑士宁愿他们只保存在肖像模式。或者,如果要检测当前方向并采取一致行动,您只需在横向模式下保存图像,对吗@末日骑士宁愿他们只保存在肖像模式。或者,为了检测当前的方向并采取一致的行动,在方向强制为纵向的情况下,这是否仍然有效?(旋转屏幕会导致不必要的一切重新创建-导致延迟和糟糕的动画旋转效果)。查看getrotation代码,它似乎是在检查手机方向,而不是我的应用程序我会试一试,看起来很有前途这是一个处理定向的想法,如果需要,只需根据您的要求进行定制。这仍然只获得应用程序定向。由于现在固定为便携式,出于速度原因,拍照时采用纵向。不确定如何获得手机+1的实际方位以获得最佳答案,该答案包含OP所需的一切-1感谢你甚至没有说“谢谢”。。。或者把它作为正确的答案来接受。这在被迫描绘方向的情况下仍然有效吗?(旋转屏幕会导致不必要的一切重新创建-导致延迟和糟糕的动画旋转效果)。查看getrotation代码,它似乎是在检查手机方向,而不是我的应用程序我会试一试,看起来很有前途这是一个处理定向的想法,如果需要,只需根据您的要求进行定制。这仍然只获得应用程序定向。由于现在固定为便携式,出于速度原因,拍照时采用纵向。联合国