我正在开发一个应用程序,我必须实现自定义相机。我遇到了一个问题。
我拍照,保存,然后使用Retrofit 2将其上传到服务器。
我得到了这样的相机方向:
private int getRotation(int id) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(id, info);
return info.orientation;
}
这适用于预览 - 预览方向正常。
mCamera.setDisplayOrientation(rotation);
相机的旋转设置如下:
parameters.setRotation(rotation);
我检查了保存的照片,照片的方向还可以。
我在3台设备上测试了它 - 华为Y5和Nexus 5X - 保存和上传文件到服务器的方向是可以的。现在奇怪的pard - 第三部手机三星Galaxy J5正确保存文件的方向,但服务器上传文件的方向是错误的。保存的照片方向正确,我可以在手机的文件管理器中看到它,但上传到服务器的图像方向错误,无法在代码中设置相机的旋转。
从相机获取图像:
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// restart preview
restartPreview();
// save image to the file
File pictureFile = getOutputMediaFile();
if(pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
uploadFile(pictureFile);
} catch(FileNotFoundException e) {
Logcat.d("FileNotFoundException");
} catch(IOException e) {
Logcat.d("IOException");
}
}
};
private void uploadFile(File file) {
AppName.getInstance().photo(mLastLocation, file, new Callback<Void>() {
@Override
public void onResponse(Call call, Response response) {
Logcat.d("CapturingService uploadFile onResponse: " + response.raw());
if(mCapture) {
// count delay to take another photo
.....
mHandler.postDelayed(mRunnable,delay);
}
}
@Override
public void onFailure(Call call, Throwable t) {
Logcat.d("CapturingService uploadFile onFailure: " + t.getMessage());
}
});
}