2012-03-09 85 views
0

我遇到了一個問題,現在我被困在很長時間,我希望你們能幫助我。 我得到了一段代碼,可以打開圖像,更改飽和度(使用seekbar)。那麼我想保存更改後的圖像。但是最後一部分並不適合。使用此代碼,我可以更改飽和度,然後保存更改後的位圖,但它將ImageView的大小保存爲位圖(480 x 517),但是他確實應用了飽和度更改(這很好)。無法保存已更改的位圖(已更改飽和度)

ColorMatrix cm = new ColorMatrix(); 

System.out.println(progress2); 
cm.setSaturation(progress2); 
ColorFilter = new ColorMatrixColorFilter(cm); 
bitmap_image = BitmapFactory.decodeFile(filePath); 
ImageView img = (ImageView) findViewById(R.id.gallery1); 
img.setColorFilter(ColorFilter); 
img.setImageBitmap(bitmap_image); 
img.buildDrawingCache(true); 
img.setDrawingCacheEnabled(true); 
Bitmap b = img.getDrawingCache(); 


Bitmap resizedBitmap = Bitmap.createBitmap(bitmap_image, 0, 0, 3264, 2448); 
File file = new File(sdcard+"/DCIM/Camera/image3.jpg"); 
try { 
    file.createNewFile(); 
    FileOutputStream ostream = new FileOutputStream(file); 
    **b.compress(CompressFormat.JPEG, 100, ostream);** 
    ostream.close(); 
} catch (Exception e) 
      { 
      System.out.println("WRITING HAS FAILED"); 
      e.printStackTrace(); 
      } 

而下一個代碼,我也可以改變飽和度,但是當我保存位圖時,它確實保存了原始高度和寬度(3264 x 2448)的位圖,但不保存飽和度更改。

ColorMatrix cm = new ColorMatrix(); 

System.out.println(progress2); 
cm.setSaturation(progress2); 
ColorFilter = new ColorMatrixColorFilter(cm); 
bitmap_image = BitmapFactory.decodeFile(filePath); 
ImageView img = (ImageView) findViewById(R.id.gallery1); 
img.setColorFilter(ColorFilter); 
img.setImageBitmap(bitmap_image); 
img.buildDrawingCache(true); 
img.setDrawingCacheEnabled(true); 
Bitmap b = img.getDrawingCache(); 


Bitmap resizedBitmap = Bitmap.createBitmap(bitmap_image, 0, 0, 3264, 2448); 
File file = new File(sdcard+"/DCIM/Camera/image3.jpg"); 
try { 
    file.createNewFile(); 
    FileOutputStream ostream = new FileOutputStream(file); 
    **resizedBitmap.compress(CompressFormat.JPEG, 100, ostream);** 
    ostream.close(); 
    } catch (Exception e) 
     { 
     System.out.println("WRITING HAS FAILED"); 
     e.printStackTrace(); 
     } 

但我需要的是可以節省的飽和度變化(代碼1),並保持原來的寬度 - 高度(代碼2),但不知何故,我似乎並沒有得到這些代碼2一起工作。

請把我放在好的方向。代碼可能是一團糟,嘗試了很多東西,結構並沒有越來越好。

**code** that is the line that changed. 

編輯: 當我這樣做:(瑞奇的建議)

Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, 3264, 2448); 

我得到這個錯誤日誌:

03-09 15:03:02.880: E/AndroidRuntime(847): FATAL EXCEPTION: main 
03-09 15:03:02.880: E/AndroidRuntime(847): java.lang.NullPointerException 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.graphics.Bitmap.createBitmap(Bitmap.java:409) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.graphics.Bitmap.createBitmap(Bitmap.java:383) 
03-09 15:03:02.880: E/AndroidRuntime(847): at com.dlv.groenmeter.GroenMeterActivity$13.onStopTrackingTouch(GroenMeterActivity.java:937) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.widget.SeekBar.onStopTrackingTouch(SeekBar.java:115) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:303) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.view.View.dispatchTouchEvent(View.java:3938) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942) 
03-09 15:03:02.880: E/AndroidRuntime(847): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1730) 
03-09 15:03:02.880: E/AndroidRuntime(847): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1142) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.app.Activity.dispatchTouchEvent(Activity.java:2102) 
03-09 15:03:02.880: E/AndroidRuntime(847): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1714) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2218) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.view.ViewRoot.handleMessage(ViewRoot.java:1889) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.os.Looper.loop(Looper.java:123) 
03-09 15:03:02.880: E/AndroidRuntime(847): at android.app.ActivityThread.main(ActivityThread.java:3691) 
03-09 15:03:02.880: E/AndroidRuntime(847): at java.lang.reflect.Method.invokeNative(Native Method) 
03-09 15:03:02.880: E/AndroidRuntime(847): at java.lang.reflect.Method.invoke(Method.java:507) 
03-09 15:03:02.880: E/AndroidRuntime(847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
03-09 15:03:02.880: E/AndroidRuntime(847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
03-09 15:03:02.880: E/AndroidRuntime(847): at dalvik.system.NativeStart.main(Native Method) 
+0

七號線(標有**「代碼'**),這是唯一的改變,其餘的代碼是相同的IDD,但它應該給我更好的視角。 – Bigflow 2012-03-09 14:01:45

+0

我現在看到了,thnx – Caner 2012-03-09 14:11:57

回答

1

有2個問題,第二個代碼:

1) 你的飽和圖像b所以resizedBitmap應該從創建這樣的:

Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, 3264, 2448); 

2) 你打造後啓用DrawingCache

img.buildDrawingCache(true); 
img.setDrawingCacheEnabled(true); 

它應該是相反的順序:

img.setDrawingCacheEnabled(true); 
img.buildDrawingCache(true); 

編輯:

試試這個代碼來設置飽和度:從下

Bitmap bmp = BitmapFactory.decodeFile(filePath); 
    Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig()); 
    Canvas canvas = new Canvas(alteredBitmap); 
    Paint paint = new Paint(); 
    ColorMatrix cm = new ColorMatrix(); 
    cm.setSaturation(progress2); 
    paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
    Matrix matrix = new Matrix(); 
    canvas.drawBitmap(bmp, matrix, paint); 

Bitmap resizedBitmap = Bitmap.createBitmap(alteredBitmap, 0, 0, 3264, 2448); 
File file = new File(sdcard+"/DCIM/Camera/image3.jpg"); 
try { 
    file.createNewFile(); 
    FileOutputStream ostream = new FileOutputStream(file); 
    resizedBitmap.compress(CompressFormat.JPEG, 100, ostream); 
    ostream.close(); 
    } catch (Exception e) 
     { 
     System.out.println("WRITING HAS FAILED"); 
     e.printStackTrace(); 
     } 
+0

** 1)**導致上面我張貼的logcat發生同樣的錯誤,不知道爲什麼它說'b'是'null',因爲我的第一個代碼確實工作(我使用'b' ) ** 2)**也改變了這一點,謝謝你已經:) – Bigflow 2012-03-09 14:20:59

+0

這意味着'b'爲空,第一個代碼可以工作,因爲'b'永遠不會在那裏使用。我認爲你的飽和改變代碼不起作用。你確定它在#1中有效嗎? – Caner 2012-03-09 14:32:04

+0

看到我編輯的答覆 – Caner 2012-03-09 14:34:46

0

從我可以看到變量 'B'包含飽和圖像。它看起來像你應該改變行

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap_image, 0, 0, 3264, 2448); 

要:

Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, 3264, 2448); 

這是否對你的工作?

+0

是的idd,我也這麼認爲,但是它保存了0kb(空)的圖像。所以soemthing仍然是錯誤的 – Bigflow 2012-03-09 14:00:55