0

I'm having problems in the following context, I need to make signatures on a form that is an image in PNG or JPG, this functionality belongs to a mobile application with Android system. The status is in the following situation, I can place the signature in the place where the user clicked. But I'm having trouble resizing the signature bitmap proportionally to the amount of zoom that was given. You need to position the signature Bitmap at the point the user clicked on, but referring to the image and not the screen.

I have browsed a lot of topics on the internet, many here in StackOverflow, but I do not find any that gives me any idea how I can do such an activity. I have already arrived several times on the third page of Google.

Can anyone help me, or can you give me some insight into how I can do this?

problem sketch

My code

public class SignatureActivity extends Activity implements OnClickListener {

private LinearLayout mContent;
private Button mClear, mSave, mCancel;
private View view;
private Dialog dialog;
private Signature mSignature;
private TouchImageView img;

private float xPosition;
private float yPosition;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_signature);


    img = (TouchImageView) findViewById(R.id.iv_form);
    img.setImageResource(R.drawable.imp_009_tiss_1200x859);
    img.setOnClickListener(this);


    // Dialog Function
    dialog = new Dialog(SignatureActivity.this);

    // Removing the features of Normal Dialogs
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_signature);
    dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    mSignature = (Signature) dialog.getWindow().findViewById(R.id.signatureView1);
    dialog.setCancelable(true);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.iv_form:
        dialog_action();
        break;
    default:
        break;
    }
}

public void carregaComponentes() {
    mContent = (LinearLayout) dialog.findViewById(R.id.ll_area_assinatura);
    mClear = (Button) dialog.findViewById(R.id.clear);
    mSave = (Button) dialog.findViewById(R.id.save);
    mCancel = (Button) dialog.findViewById(R.id.cancel);
    mSignature = (Signature) dialog.findViewById(R.id.signatureView1);
}

public void dialog_action() {

    carregaComponentes();
    mSignature.setBackgroundColor(Color.WHITE);
    mSignature.setSigColor(255, 0, 0, 255);

    view = mContent;

    mClear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v("log_tag", "Panel Cleared");
            mSignature.clearSignature();

        }
    });

    mSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v("log_tag", "Panel Saved");
            view.setDrawingCacheEnabled(true);

            if (saveSignature(view)) {
                dialog.dismiss();
            } else {
                mSignature.clearSignature();
            }

        }
    });

    mCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v("log_tag", "Panel Canceled");
            dialog.dismiss();
            mSignature.clearSignature();

        }
    });
    dialog.show();

}

public boolean saveSignature(View view) {
    DisplayMetrics dm = new DisplayMetrics();
    float dpi = dm.scaledDensity;

    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int screenW = dm.widthPixels;
    int screenH = dm.heightPixels;

    Bitmap assinatura = mSignature.getImage();
    Bitmap assinaturaRedimensionada = Bitmap.createScaledBitmap(
            assinatura,
            (Integer.valueOf((int) (screenW * 0.20))),
            (Integer.valueOf((int) (screenH * 0.070))),
            false);

    img.buildDrawingCache();

    Bitmap imgGto = img.getDrawingCache();

    Bitmap gtoAssinada = Bitmap.createBitmap(imgGto.getWidth(), imgGto.getHeight(), imgGto.getConfig());

    Canvas canvas = new Canvas(gtoAssinada);
    canvas.drawBitmap(imgGto, new Matrix(), null);

    canvas.drawBitmap(assinaturaRedimensionada, TouchImageViewListener.x, TouchImageViewListener.y, null);

    // Creating Separate Directory for saving Generated Images
    File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    String pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String StoredPath = "GTO_assinada" + pic_name + ".png";
    File fichero = new File(sd, StoredPath);

    try {
        if (sd.canWrite()) {
            fichero.createNewFile();
            OutputStream os = new FileOutputStream(fichero);
            gtoAssinada.compress(Bitmap.CompressFormat.PNG, 90, os);
            os.close();

            img.setImageBitmap(gtoAssinada);
            mSignature.clearSignature();
            Toast.makeText(getApplicationContext(), "GTO Assinada com Sucesso!", Toast.LENGTH_LONG).show();

        }
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Valdeco
  • 19
  • 4
  • so you want two `Bitmap`s to be synchronized? i mean when the big PNG or JPG zooms or pans your small "signature" `Bitmap` should zoom and pan in sync? – pskink Sep 11 '18 at 14:26
  • the image you added does not explain anything... what actually do you want to achieve? – pskink Sep 11 '18 at 14:51
  • 1
    I have an image, which represents a form. this image has a point that should receive a signature. and I'm having trouble identifying the point of the image that was clicked and put the signature at that point. but taking into consideration that I'm with Zoom. – Valdeco Sep 11 '18 at 15:01
  • 1
    that's more or less what you said @pskink – Valdeco Sep 11 '18 at 15:03
  • post your code with click/touch listeners – pskink Sep 11 '18 at 15:16
  • @pskink , follow my code. – Valdeco Sep 11 '18 at 15:56

0 Answers0