开发者

Android bitmap compression is not good

I got the following output stream codes:

        String output_file = APP_FILE_PATH + "/AudienceSignatures/" + CaptureSignature.this.sessionNumber + ".png";

        final FileOutputStream out = new FileOutputStream(new File( output_file ));
        nBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();

but it seemed like the resulting image is not what i am expecting. it has some sort of lines as you can see, i want to get rid of those horizontal white line. what could be the cause of this?

Android bitmap compression is not good

Thanks a lot for any help you may give! :)

UPDATE: Here's the CaptureSignature.java class where 'i think' I'm having a problem with:

package com.first.MyApp.drawings;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.first.Engagia.Camera;
import com.first.Engagia.R;
import com.first.Engagia.R.id;
import com.first.Engagia.R.layout;
import com.first.Engagia.drawings.brush.Brush;
import com.first.Engagia.drawings.brush.CircleBrush;
import com.first.Engagia.drawings.brush.PenBrush;

import java.io.File;
import java.io.FileOutputStream;

public class CaptureSignature extends Activity implements View.OnTouchListener{
    private DrawingSurface drawingSurface;
    private DrawingPath currentDrawingPath;
    private Paint currentPaint;

    private Brush currentBrush;

    private File APP_FILE_PATH = new File(Environment.getExternalStorageDirectory() + "/Engagia/AudienceSignatures");

    //..some other instance variables here

    public static final String LOG_TAG = "-------->>>> CAPTURE SIGNATURE <<<<-------";
    private ProgressDialog mProgressDialog;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawing_activity);

        Log.d(LOG_TAG, "Inside capture signature");

        PopIt("Camera", "Please sign on the whitespace provided.");

        Bundle extras = getIntent().getExtras(); 

        if(extras != null){
            this.userId = extras.getString("userId");
            this.appview_username = extras.getString("username");
            this.appview_password = extras.getString("password");

            this.userfirstname = extras.getString("userfirstname");
            this.userlastname = extras.getString("userlastname");
            this.companyname = extras.getString("companyname");

            this.sessionNumber = extras.getString("sessionNumber");
            this.sessionFirstname = extras.getString("sessionFirstname");
            this.sessionLastname = extras.getString("sessionLastname");

            this.AudienceFirstnameLastname = extras.getString("AudienceFirstnameLastname");

        }

        setCurrentPaint();
        currentBrush = new PenBrush();

        drawingSurface = (DrawingSurface) findViewById(R.id.drawingSurface);
        drawingSurface.setOnTouchListener(this);
        drawingSurface.previewPath = new DrawingPath();
        drawingSurface.previewPath.path = new Path();
        drawingSurface.previewPath.paint = getPreviewPaint();


    }

    public void PopIt(String title, String message){
        android.content.DialogInterface.OnClickListener arg1 = null;
        new AlertDialog.Builder(this)
        .setTitle( title )
        .setMessage( message )
        .setPositiveButton("OK", arg1).show();
    }


    private void setCurrentPaint(){
        currentPaint = new Paint();
        currentPaint.setDither(true);
        currentPaint.setColor(0xff000000);
        currentPaint.setStyle(Paint.Style.STROKE);
        currentPaint.setStrokeJoin(Paint.Join.ROUND);
        currentPaint.setStrokeCap(Paint.Cap.ROUND);
        currentPaint.setStrokeWidth(8);

    }

    private Paint getPreviewPaint(){
        final Paint previewPaint = new Paint();
        previewPaint.setColor(0xff000000);
        previewPaint.setStyle(Paint.Style.STROKE);
        previewPaint.setStrokeJoin(Paint.Join.ROUND);
        previewPaint.setStrokeCap(Paint.Cap.ROUND);
        previewPaint.setStrokeWidth(8);
        return previewPaint;
    }




    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
            drawingSurface.isDrawing = true;

            currentDrawingPath = new DrawingPath();
            currentDrawingPath.paint = currentPaint;
            currentDrawingPath.path = new Path();
            currentBrush.mouseDown(currentDrawingPath.path, motionEvent.getX(), motionEvent.getY());
            currentBrush.mouseDown(drawingSurface.previewPath.path, motionEvent.getX(), motionEvent.getY());


        }else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
            drawingSurface.isDrawing = true;
            currentBrush.mouseMove( currentDrawingPath.path, motionEvent.getX(), motionEvent.getY() );
            currentBrush.mouseMove(drawingSurface.previewPath.path, motionEvent.getX(), motionEvent.getY());


        }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){


            currentBrush.mouseUp(drawingSurface.previewPath.path, motionEvent.getX(), motionEvent.getY());
            drawingSurface.previewPath.path = new Path();
            drawingSurface.addDrawingPath(currentDrawingPath);

            currentBrush.mouseUp( currentDrawingPath.path, motionEvent.getX(), motionEvent.getY() );

        }

        return true;
    }


    public void onClick(View view){
        switch (view.getId()){
            case R.id.saveBtn:
                Log.d(LOG_TAG, "Save Button clicked!");


                showDialog(0);
                CaptureSignature.this.mProgressDialog.setMessage("Saving your signature...");

                final Activity currentActivity  = this;
                Handler saveHandler = new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        final AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
                        alertDialog.setTitle("Done");
                        alertDialog.setMessage("Your signature has been captured.");
                        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                Log.d(LOG_TAG, "Going to camera activity");

                                //...intent to next activity after signature was taken

                                return;
                            }
                        });

                        if( CaptureSignature.this.mProgressDialog.isShowing() ){
                            dismissDialog(0);
                        }

                        alertDialog.show();
                    }
                } ;
               new ExportBitmapToFile(this,saveHandler, drawingSurface.getBitmap()).execute();
            break;
            case R.id.resetBtn:
                Log.d(LOG_TAG, "Reset Button clicked!");

                //..reset intent here

                break;

        }
    }


    private class ExportBitmapToFile extends AsyncTask<Intent,Void,Boolean> {
        private Context mContext;
        private Handler mHandler;
        private Bitmap nBitmap;

        public ExportBitmapToFile(Context context,Handler handler,Bitmap bitmap) {
            mContext = context;
            nBitmap = bitmap;
            mHandler = handler;
        }

        @Override
        protected Boolean doInBackground(Intent... arg0) {
            try {
                if (!APP_FILE_PATH.exists()) {
                    APP_FILE_PATH.mkdirs();
                }
         开发者_StackOverflow中文版       Log.d(LOG_TAG, "Sig.output stream area.");

                final FileOutputStream out = new FileOutputStream(new File(APP_FILE_PATH + "/" +  CaptureSignature.this.sessionNumber + ".png"));
                nBitmap.setDensity(50);
                nBitmap.compress(Bitmap.CompressFormat.PNG, 50, out);

                out.flush();
                out.close();

                Log.d(LOG_TAG, "Done bitmap compress.");
                return true;
            }catch (Exception e) {
                e.printStackTrace();
            }

            return false;
        }


        @Override
        protected void onPostExecute(Boolean bool) {
            super.onPostExecute(bool);
            if ( bool ){
                mHandler.sendEmptyMessage(1);
            }
        }
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case 0:
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mProgressDialog.show();
                return mProgressDialog;
            default:
                return null;
        }
    }
}

Basically, I'm trying to capture the user's signature and save it to png file in my android device sdcard.


I have never seen such a problem with our PNG compression. Couldn't it be the original bitmap?


The original bitmap may have lines due to a dirty or oily screen when the person made the signature. I also saw a screen with lines like this when it was going defective.


The onTouch method has a lot of things going on inside it (like memory allocations- using new). This seems the root cause of your problem. The onTouch is called many times a second when a user touches the screen. If this method doesn't return in time, the next call to onTouch does not take place. That is probably why you have a few missing "strokes" in your resultant image. Clean up the code and see the results. If that doesn't help, or even otherwise, you can use my code:

public boolean onTouchEvent(MotionEvent event){
    final int source = event.getSource();
    if(source!=InputDevice.SOURCE_TOUCHSCREEN &&
            source!=InputDevice.SOURCE_MOUSE &&
            source!=InputDevice.SOURCE_TOUCHPAD){
        Log.v(TAG, "returns false");

        return false;
    }

    switch(event.getActionMasked()){
    case MotionEvent.ACTION_DOWN:
        path.reset();
        path.moveTo(event.getX(), event.getY());
        break;
    case MotionEvent.ACTION_MOVE:
        path.lineTo(event.getX(), event.getY());
        break;
    case MotionEvent.ACTION_UP:
        path.lineTo(event.getX(), event.getY());
        charDrawn();
    }
    invalidate();//omit this. I need it since it calls the onDraw() method
    Log.v(TAG, "returns true");
    return true;
}

I have created my own "View" by extending the View class. I just extract the path while the user is moving his finger. Since I use path.lineTo my path is never broken. I get a thin line. Once the path is drawn I can do what ever I want with it (in the charDrwan() method). I create a bitmap by creating a Canvas and using its canvas.drawPath(Path,Paint) method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜