I got a problem, where I am stuck now for a long time, I hope you guys can help me out.
I got a piece of code that I can open an image, change the saturation (with seekbar). then I want to save the changed image. But that last part isn’t working as it should be.
With this code, I can change saturation, and then save the changed bitmap, but it saves the bitmap with the size of the ImageView (480 x 517) however he does apply the saturation change (that is good)
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();
}
And the next code, I can also change the saturation, but when I save the bitmap, it does save the bitmap with the original height and width (3264 x 2448) but it doesn’t save the saturation change.
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();
}
But what I need is that it saves the saturation changes (code 1) and keeps the original width – height (code 2), but somehow I don’t seem to get those 2 codes work together.
Please put me in the good direction.
Code is maybe a mess, tried a lot of things and the structure isn’t getting better of it.
**code** that is the line that changed.
EDIT:
When I do this: (suggestion of Ricki)
Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, 3264, 2448);
I get this error log:
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)
There are 2 problems with the 2nd code:
1)
Your saturated image is
bsoresizedBitmapshould be created from that like this:2)
You enable
DrawingCacheafter you build:It should be in reverse order:
EDIT:
Try this code to set the saturation: