【问题标题】:takePicture() landscape mode rotation androidtakePicture() 横向模式旋转 android
【发布时间】:2016-07-03 21:34:29
【问题描述】:

我正在使用相机拍摄图像:

mCamera.takePicture(null, null,
                            new PhotoHandler(getApplicationContext()));

但是我拍摄的图像是横向的,我想要纵向的,我尝试将文件转换为位图,旋转它然后再次保存,这是我的onPictureTaken() 函数和rotate() 函数在PhotoHandler.class 中:

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    File pictureFileDir = getDir();
    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
        Log.d("Error", "Can't create directory to save image.");
        Toast.makeText(context, "Can't create directory to save image.",
                Toast.LENGTH_LONG).show();
        return;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
    String photoFile = "Picture_" + date + ".jpg";
    String filename = pictureFileDir.getPath() + File.separator + photoFile;
    pictureFile = new File(filename);
    st1 = pictureFile.getAbsolutePath();
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
        Toast.makeText(context, "New Image saved:" + photoFile,
                Toast.LENGTH_LONG).show();
    } catch (Exception error) {
        Log.d("Error", "File" + filename + "not saved: "
                + error.getMessage());
        Toast.makeText(context, "Image could not be saved.",
                Toast.LENGTH_LONG).show();
    }
    Bitmap b = rotate(BitmapFactory.decodeFile(pictureFile.getAbsolutePath()),270);
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(pictureFile);
        b.compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    CameraPreview.safeToTakePicture = true;
    galleryAddPic();
}

public static Bitmap rotate(Bitmap bitmap, int degree) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Matrix mtx = new Matrix();
    mtx.setRotate(degree);
    return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

反正时间太长了,我试过mCamera.setDisplayOrientation(90);但是只改变了相机的预览,拍出来的照片还是一样的,我也尝试了获取相机的参数并像这里一样改变它:

Camera.Parameters parameters = mCamera.getParameters();
parameters.set("orientation", "portrait");
mCamera.setParameters(parameters);

如果没有成功,我们将不胜感激。

编辑: 新代码:

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    File pictureFileDir = getDir();
    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
        Log.d("Error", "Can't create directory to save image.");
        Toast.makeText(context, "Can't create directory to save image.",
                Toast.LENGTH_LONG).show();
        return;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
    String date = dateFormat.format(new Date());
    String photoFile = "Picture_" + date + ".jpg";
    String filename = pictureFileDir.getPath() + File.separator + photoFile;
    pictureFile = new File(filename);
    st1 = pictureFile.getAbsolutePath();
    Bitmap b = rotate(BitmapFactory.decodeByteArray(data,0,data.length),270);
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(pictureFile);
        b.compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    CameraPreview.safeToTakePicture = true;
    galleryAddPic();
}  

【问题讨论】:

    标签: android


    【解决方案1】:

    反正时间太长了

    您当前的算法如下所示:

    • 从记忆中的照片开始
    • 将照片保存到磁盘,速度很慢
    • 将照片从磁盘读回内存,速度很慢
    • 旋转重新加载的照片

    更有效的方法是:

    • 从记忆中的照片开始
    • 旋转照片

    BitmapFactory 具有 decodeByteArray() 以获取 Bitmap 用于轮换目的。

    【讨论】:

    • @DAVIDBALAS1:如果您的目标是保存旋转后的照片,那么您所拥有的应该比以前更有效率。
    • 是的,我正在尝试保存图像。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-07-31
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    相关资源
    最近更新 更多