我有一个问题,我正在编写缩放位图的代码并创建新的Bitmap usint Bitmap.createBitmap()方法,该方法始终返回一个我无法识别的错误。请建议我任何解决方案。
ErrorStack:
02-22 16:27:14.679: E/AndroidRuntime(10363): FATAL EXCEPTION: main
02-22 16:27:14.679: E/AndroidRuntime(10363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TestFace.FaceDetect/com.TestFace.FaceDetect.ProcessImage}: java.lang.IllegalArgumentException: width and height must be > 0
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.os.Looper.loop(Looper.java:123)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-22 16:27:14.679: E/AndroidRuntime(10363): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 16:27:14.679: E/AndroidRuntime(10363): at java.lang.reflect.Method.invoke(Method.java:521)
02-22 16:27:14.679: E/AndroidRuntime(10363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-22 16:27:14.679: E/AndroidRuntime(10363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-22 16:27:14.679: E/AndroidRuntime(10363): at dalvik.system.NativeStart.main(Native Method)
02-22 16:27:14.679: E/AndroidRuntime(10363): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.graphics.Bitmap.nativeCreate(Native Method)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
02-22 16:27:14.679: E/AndroidRuntime(10363): at com.TestFace.FaceDetect.ProcessImage.scaleImage(ProcessImage.java:247)
02-22 16:27:14.679: E/AndroidRuntime(10363): at com.TestFace.FaceDetect.ProcessImage.onCreate(ProcessImage.java:67)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-22 16:27:14.679: E/AndroidRuntime(10363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-22 16:27:14.679: E/AndroidRuntime(10363): ... 11 more
代码:
private Bitmap scaleImage(Bitmap paramBitmap) {
// TODO Auto-generated method stub
int i = paramBitmap.getWidth();
System.out.println("Width========="+i);
int j = paramBitmap.getHeight();
System.out.println("Height========="+j);
float f2 = j / i * 300;
float f1 = 300 / i;
f2 /= j;
Matrix localMatrix = new Matrix();
localMatrix.postScale(f1, f2);
return Bitmap.createBitmap(paramBitmap, 0, 0, i, j, localMatrix, false);
}
答案 0 :(得分:3)
Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);