无法保存更改位图(更改饱和度)

时间:2012-03-09 13:41:44

标签: java android image bitmap save

我遇到了问题,我现在被困了很长时间,我希望你们能帮助我。 我得到了一段代码,我可以打开图像,改变饱和度(使用搜索栏)。然后我想保存更改的图像。但是最后一部分不能正常运作。

使用此代码,我可以更改饱和度,然后保存更改的位图,但它会保存具有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),但不知怎的,我似乎没有让这两个代码一起工作。

请把我放在好方向。 代码可能是一团糟,尝试了很多东西,结构也没有变得更好。

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

编辑: 当我这样做时:( Ricki的建议)

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)

2 个答案:

答案 0 :(得分:1)

第二个代码有两个问题:

<强> 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();
        }

答案 1 :(得分:0)

从我所看到的变量'b'包含饱和图像。看起来你应该更改行

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

要:

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

这对你有用吗?