android - takePicture() 横向模式旋转android

标签 android

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

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();
}  

最佳答案

Anyway, it takes too long

您当前的算法如下所示:

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

更有效的方法是:

  • 从内存中的照片开始
  • 旋转照片

BitmapFactorydecodeByteArray() 来获取 Bitmap 以进行旋转。

关于android - takePicture() 横向模式旋转android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170287/

相关文章:

android - 为什么 RSSurfaceView 类这么快就被弃用了?

java - Option + Return 不会导入缺失的类 "creates test"

android - SlidingDrawer 动画速度

android - 如何编写通用 BindingAdapter 来设置 OnLongClickListener?

android - TextInputLayout 密码切换从中心移动文本

Android使用Zxing库扫描二维码崩溃

android - 如何写血压google fit

java - BigDecimal 导致 NumberFormatException

android - 如何激活三星S Health开发者模式?

android - 无法使用Android应用程序在mesibo中创建用户