Android Desire HD 位图缩放
我构建了一个自定义相机活动,用户可以在其中拍照。通过图片回调将图片保存到外部存储目录中。然后,用户被重定向到另一个活动,他可以在其中预览图片。在显示预览之前,图片会在单独的线程中按比例缩小到某个最大尺寸。这是缩放代码:
byte[] tempStorage = new byte[16 * 1024];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
int oldWidth = options.outWidth;
int oldHeight = options.outHeight;
if (oldWidth > MAX_DIM || oldHeight > MAX_DIM) {
Logger.i(Constants.SCALE_TAG, "Must scale picture");
int scaleFactor = getScaleFactor(oldWidth, oldHeight);
options.inJustDecodeBounds = false;
options.inSampleSize = scaleFactor;
options.inTempStorage = tempStorage;
System.gc();
newB = BitmapFactory.decodeFile(imagePath, options);
} else {
Logger.i(Constants.SCALE_TAG, "No need for scaling");
// We do not need to scale the picture, it is small
// enough.
System.gc();
options.inJustDecodeBounds = false;
options.inTempStorage = tempStorage;
if (!stop) {
newB = BitmapFactory.decodeFile(imagePath, options);
} else {
return;
}
}
这是计算缩放系数的代码:
private int getScaleFactor(int oldWidth, int oldHeight) {
// Holds the raw scale factor.
float rawScale;
// Get the scaling factor.
if (oldWidth >= oldHeight) {
rawScale = 1.0F / ((MAX_DIM * 1.0F) / (oldWidth * 1.0F));
} else {
rawScale = 1.0F / ((MAX_DIM * 1.0F) / (oldHeight * 1.0F));
}
Logger.i(Constants.SCALE_TAG, "Raw scale factor: " + rawScale);
return Math.round(rawScale);
}
该代码在大多数设备上运行良好,但不知何故 Desire HD 无法正确缩放图片。看起来他把原始图片复制了三十次左右,然后将其压缩到位图中,形成了奇怪的条纹状图片。有人知道这个问题吗?
I've build a custom camera activity where the user can take a picture. The picture is saved in the external storage directory via the picture callback. The user is then redirected to another activity where he can preview the picture. Before the preview is shown, the picture is scaled down in a seperate thread to a certain maximum dimension. Here is the scaling code:
byte[] tempStorage = new byte[16 * 1024];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
int oldWidth = options.outWidth;
int oldHeight = options.outHeight;
if (oldWidth > MAX_DIM || oldHeight > MAX_DIM) {
Logger.i(Constants.SCALE_TAG, "Must scale picture");
int scaleFactor = getScaleFactor(oldWidth, oldHeight);
options.inJustDecodeBounds = false;
options.inSampleSize = scaleFactor;
options.inTempStorage = tempStorage;
System.gc();
newB = BitmapFactory.decodeFile(imagePath, options);
} else {
Logger.i(Constants.SCALE_TAG, "No need for scaling");
// We do not need to scale the picture, it is small
// enough.
System.gc();
options.inJustDecodeBounds = false;
options.inTempStorage = tempStorage;
if (!stop) {
newB = BitmapFactory.decodeFile(imagePath, options);
} else {
return;
}
}
Here is the code that calculates the scaling factor:
private int getScaleFactor(int oldWidth, int oldHeight) {
// Holds the raw scale factor.
float rawScale;
// Get the scaling factor.
if (oldWidth >= oldHeight) {
rawScale = 1.0F / ((MAX_DIM * 1.0F) / (oldWidth * 1.0F));
} else {
rawScale = 1.0F / ((MAX_DIM * 1.0F) / (oldHeight * 1.0F));
}
Logger.i(Constants.SCALE_TAG, "Raw scale factor: " + rawScale);
return Math.round(rawScale);
}
This code works fine on most devices, but somehow the Desire HD won't correctly scale the picture. It looks like he takes the original picture and duplicates it thirty times or so and squizes it into the Bitmap, causing a weird stripe-like picture. Does anyone know of this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该使用更少的内存。问题是你使用了太多内存。
You should use less memory. The problem is you're using too much memory.