android - 如何缩小固定宽度的照片?
问题描述
我使用fotoapparat.io 库来拍摄我的照片。由于我不需要那些 12 或 15 兆像素的全分辨率,我想将该图像缩小到“768 x H”,而 H 是我的高度,取决于相机的纵横比;通常是 4:3 所以 576 像素。
既不调用:
photoResult.toBitmap(
new Function1<Resolution, Resolution>() {
@Override
public Resolution invoke(Resolution resolution) {
float scale = (float) 768 / resolution.width;
return scaled(scale).invoke(resolution);
}
}
)
也没有缩放:
photoResult.toBitmap(
new Function1<Resolution, Resolution>() {
@Override
public Resolution invoke(Resolution resolution) {
int height = 768;
int width = 576;
Resolution r = new Resolution(width, height);
return scaled(1.0F).invoke(r);
}
}
)
对我有帮助。第二个例子,当使用固定的 1.0F 作为缩放因子时,它会扭曲图像。或者 4/3 作为缩放因子,图像具有良好的比例,但随后缩小到 576 x 432 像素。
如何在不改变不同相机的纵横比的情况下将图像缩小到固定宽度?
解决方案
我有 java 代码来重新缩放位图。我用它把我的图像缩小到“600 x H”。
private Bitmap rescaleBitmap(Bitmap bitmap) {
float constant = 600.0f / bitmap.getWidth();
return Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * constant),
(int) (bitmap.getHeight() * constant), true);
}
推荐阅读
- vba - 当表格中的单元格不为空时添加行
- c++ - 输出字符串数组
- inform7 - If statement won't work in Inform 7
- java - Android Studio Gradle Daemon - java.net.BindException: Address already in use: bind
- python - Image processing - eliminate arc-like smears
- reactjs - React.createRef always null
- swift - Occasional stuttering with SceneKit when touching the screen
- android - Is it possible to use 2 Crashlytics API keys?
- javascript - How to pass function in another file in React
- java - Public Key Generation from .cer file not working on Android 28