2013-03-28 136 views
0

我開發的塗料應用程序,我想插入剪切功能,我用從CanvasView此代碼:如何裁剪位圖?

public void Crop() { 
    // TODO Auto-generated method stub 
    int value = 0; 
    if (bitmap.getHeight() <= bitmap.getWidth()) { 
     value = bitmap.getHeight(); 
    } else { 
     value = bitmap.getWidth(); 
    } 

    Bitmap finalBitmap = null; 
    finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, value, value); 
    Bitmap lastBitmap = null; 
    lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true); 

} 

但我recive以下錯誤

03-28 03:10:18.365: E/AndroidRuntime(19708): FATAL EXCEPTION: main 
03-28 03:10:18.365: E/AndroidRuntime(19708): java.lang.NullPointerException 
03-28 03:10:18.365: E/AndroidRuntime(19708): at baked.soft.CanvasView.Crop(CanvasView.java:670) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at baked.soft.FirstActivity$7.onClick(FirstActivity.java:157) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at android.view.View.performClick(View.java:4232) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at android.view.View$PerformClick.run(View.java:17318) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at android.os.Handler.handleCallback(Handler.java:615) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at android.os.Handler.dispatchMessage(Handler.java:92) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at android.os.Looper.loop(Looper.java:137) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at android.app.ActivityThread.main(ActivityThread.java:4921) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at java.lang.reflect.Method.invokeNative(Native Method) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at java.lang.reflect.Method.invoke(Method.java:511) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
03-28 03:10:18.365: E/AndroidRuntime(19708): at dalvik.system.NativeStart.main(Native Method) 

這是錯誤行:

從FirstActivity

if (bitmap.getHeight() <= bitmap.getWidth()) { 

位圖

+0

你在哪裏聲明位變量: -/ – 2013-03-28 01:17:36

回答

2

輸入bitmap爲空,因此爲NullPoinerException


順便問一下,你的代碼應該看起來更像是這樣的:

public void Crop() { 
    if(bitmap == null) { 
     return; 
    } 

    final int value; 
    if (bitmap.getHeight() <= bitmap.getWidth()) { 
     value = bitmap.getHeight(); 
    } else { 
     value = bitmap.getWidth(); 
    } 

    final Bitmap finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, value, value); 
    final Bitmap lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true); 
} 
+0

確定,但在FirstActivity – 2013-03-28 01:29:01

+1

檢查創建了'如果(位圖我的位圖=='null)'或者在你將它發送到'Crop()'之前,或者在'Crop()'中使用它之前。 – 2013-03-28 01:31:03

+0

'imageViewCrop =(ImageView)this.findViewById(R.id.imageViewCrop); imageViewCrop.setOnClickListener(新OnClickListener(){ \t \t \t \t \t \t公共無效的onClick(視圖v){ \t \t \t \t位圖的位圖= Bitmap.createBitmap(myView.getWidth(),myView.getHeight(), Config.ARGB_8888); \t \t \t \t帆布帆布=新畫布(位圖); \t \t \t \t myView.draw(畫布); \t \t \t \t Toast.makeText(getApplicationContext(),「重做工具單擊」,Toast.LENGTH_SHORT).show(); \t \t \t \t myView.Crop(); \t \t \t \t \t bitmap.recycle(); \t \t \t} \t \t});'這是從FirstActivity – 2013-03-28 01:34:36