我已经制作了一个相机应用程序,让我拍照但我想用特定的(预设)宽度/高度和宽高比保存它。
示例:
我尝试了以下(没有运气):
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(editPhotoFile.getAbsolutePath(), bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
Bitmap croppedBmp = Bitmap.createBitmap(bitmap, margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin);
利用我尝试完成的边距来切割左边(第一边距)和顶边(第二边距)的特定像素数量,并且在总宽度和高度上相同。不工作。
Github上也有一些库可用,但它们都让我选择一个图像并进行编辑 - 我不需要手动编辑,我想要预设边距来裁剪。
此外,Stack Overflow上的可能解决方案以及谷歌搜索或查找教程并不能给我带来任何好运。 搜索查询:
java android crop image
java android crop image tutorial
谁能帮帮我?
答案 0 :(得分:1)
我也到了那里。挣扎了几个星期,但我的大脑得到了澄清:一切都很好,但我没有将图片/图像保存到设备上。
保证金正在发挥作用。
明天将使用我找到的解决方案更新/编辑这篇文章。
<强>更新强>
// get String with path to file from other activity and make new File of it
File editPhotoFile = new File(globalFileString);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath(), bmOptions);
// crop the bitmap with new margins: bitmap, left, top, width, height
Bitmap croppedBmp = Bitmap.createBitmap(bitmap, marginLeft, marginTop, bitmapHeight, bitmapWidth);
// save bitmap to new file
FileOutputStream out = null;
File mediaFile;
try {
mediaFile = new File(globalFileString);
out = new FileOutputStream(mediaFile);
croppedBmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}