【发布时间】:2014-07-26 09:28:52
【问题描述】:
我正在使用搜索栏缩放位图。每当我增加搜索栏的进度时,图像缩放都会因错误“位图大小超过 32 位”而失败。如果我使用默认搜索栏值缩放图像。它给出了一个错误“非法参数异常:宽度和高度必须> 0”。
日志报告
07-26 05:20:23.189: E/AndroidRuntime(1145): FATAL EXCEPTION: main
07-26 05:20:23.189: E/AndroidRuntime(1145): java.lang.IllegalArgumentException: bitmap size exceeds 32bits
07-26 05:20:23.189: E/AndroidRuntime(1145): at android.graphics.Bitmap.nativeCreate(Native Method)
07-26 05:20:23.189: E/AndroidRuntime(1145): at android.graphics.Bitmap.createBitmap(Bitmap.java:697)
07-26 05:20:23.189: E/AndroidRuntime(1145): at android.graphics.Bitmap.createBitmap(Bitmap.java:674)
07-26 05:20:23.189: E/AndroidRuntime(1145): at android.graphics.Bitmap.createBitmap(Bitmap.java:607)
07-26 05:20:23.189: E/AndroidRuntime(1145): at com.example.navigationexample.QRCodeGenerator.scaleImage(QRCodeGenerator.java:127)
07-26 05:20:23.189: E/AndroidRuntime(1145): at com.example.navigationexample.MainActivity$3.onClick(MainActivity.java:356)
07-26 05:20:23.189: E/AndroidRuntime(1145): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-26 05:20:23.189: E/AndroidRuntime(1145): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 05:20:23.189: E/AndroidRuntime(1145): at android.os.Looper.loop(Looper.java:137)
07-26 05:20:23.189: E/AndroidRuntime(1145): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-26 05:20:23.189: E/AndroidRuntime(1145): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 05:20:23.189: E/AndroidRuntime(1145): at java.lang.reflect.Method.invoke(Method.java:525)
07-26 05:20:23.189: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-26 05:20:23.189: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 05:20:23.189: E/AndroidRuntime(1145): at dalvik.system.NativeStart.main(Native Method)
07-26 05:20:25.857: I/Process(1145): Sending signal. PID: 1145 SIG: 9
代码sn-p
public void showSaveDialog() {
LayoutInflater inflater = getLayoutInflater();
dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Save QR Image");
View v = inflater.inflate(R.layout.save_dialog, null);
builder.setView(v);
final EditText et = (EditText) v.findViewById(R.id.qrName);
et.setMaxLines(1);
final TextView widthText = (TextView) v.findViewById(R.id.widthSize);
final TextView heightText = (TextView) v.findViewById(R.id.heightSize);
SeekBar seekBar = (SeekBar) v.findViewById(R.id.seekBar1);
seekBar.setMax(16);
// To set minimum value of seekbar
seekMin = seekBar.getProgress();
if (seekMin < 128) {
seekBar.setProgress(1);
widthText.setText("128");
heightText.setText("128");
}
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
// TODO Auto-generated method stub
val = progresValue * 128;
String size = String.valueOf(val);
widthText.setText(size);
heightText.setText(size);
}
});
builder.setPositiveButton("Save", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
qrName = et.getText().toString();
if (qrName.isEmpty()) {
Toast.makeText(getApplicationContext(),
"Please enter name", Toast.LENGTH_SHORT).show();
} else {
Bitmap sImg = classB.scaleImage(image, val, density);
saveImage(sImg);
}
}
});
dialog = builder.create();
dialog.show();
}
B类图像缩放功能
public Bitmap scaleImage(Bitmap bitmap, int bound, int density) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Get current dimensions AND the desired bounding box
int bounding = dpToPx(bound, density);
Log.i("Test", "original width = " + Integer.toString(w));
Log.i("Test", "original height = " + Integer.toString(h));
Log.i("Test", "bounding = " + Integer.toString(bounding));
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) bounding) / w;
float yScale = ((float) bounding) / h;
float scale = (xScale <= yScale) ? xScale : yScale;
Log.i("Test", "xScale = " + Float.toString(xScale));
Log.i("Test", "yScale = " + Float.toString(yScale));
Log.i("Test", "scale = " + Float.toString(scale));
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the
// ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
true);
sWidth = scaledBitmap.getWidth(); // re-use
sHeight = scaledBitmap.getHeight(); // re-use
@SuppressWarnings("deprecation")
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
Log.i("Test", "scaled width = " + Integer.toString(sWidth));
Log.i("Test", "scaled height = " + Integer.toString(sHeight));
return scaledBitmap;
}
在 scaleImage() 函数中获取边界值的函数
private int dpToPx(int dp, int density) {
int result = Math.round((float) dp * density);
return result;
}
【问题讨论】:
-
听起来您想缩放到太大或 0。当您开始创建位图时,w、h 和缩放是什么?
-
默认搜索栏进度为 1。在此进度中,我得到“宽度和高度必须 > 0”,日志值为原始宽度 = 540,原始高度 = 540,边界 = 0,xScale = 0.0, yScale = 0.0, scale = 0.0
-
好吧,scale 为 0,这会导致问题。将任何东西缩放到 0 都会得到 0 的高度和宽度。
-
那么,有什么解决办法吗?
-
是的,无论如何,看起来边界都是 0。修复它。我的猜测是您没有在活动类中为 val 提供默认值,因此将其初始化为 0。
标签: android bitmap fatal-error illegalargumentexception