安卓开发:相机水印设置

1.更新水印

DecimalFormat DF = new DecimalFormat("#");
DecimalFormat DF1 = new DecimalFormat("#.#");

LocationManager LM = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        altitude = location.getAltitude();
        speed = location.getSpeed();
        textView_longitude.setText("经度:" + DuFenMiao(longitude));
        textView_latitude.setText("纬度:" + DuFenMiao(latitude));
        textView_altitude.setText("高度:" + DF.format(altitude) + "米");
        if (speed * 3.6 < 10)
            textView_speed.setText("速度:" + DF1.format(speed * 3.6) + "千米/时");
        else
            textView_speed.setText("速度:" + DF.format(speed * 3.6) + "千米/时");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onProviderDisabled(String provider) {}
});

2.设置水印

class ClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            ......
        case R.id.button_watermark:
            String[] items = { "经纬度", "高度", "速度", "时间" };
            boolean watermark_geo = sharedPreferences.getBoolean("watermark_geo", true);
            boolean watermark_altitude = sharedPreferences.getBoolean("watermark_altitude", true);
            boolean watermark_speed = sharedPreferences.getBoolean("watermark_speed", true);
            boolean watermark_time = sharedPreferences.getBoolean("watermark_time", true);
            boolean[] check = { watermark_geo, watermark_altitude, watermark_speed, watermark_time };
            new AlertDialog.Builder(MainActivity.this)
            .setTitle("水印")
            .setMultiChoiceItems(items, check, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    switch (i) {
                    case 0:
                        if (b) {
                            textView_longitude.setVisibility(View.VISIBLE);
                            textView_latitude.setVisibility(View.VISIBLE);
                        } else {
                            textView_longitude.setVisibility(View.GONE);
                            textView_latitude.setVisibility(View.GONE);
                        }
                        editor.putBoolean("watermark_geo", b);
                        break;
                    case 1:
                        if (b)
                            textView_altitude.setVisibility(View.VISIBLE);
                        else
                            textView_altitude.setVisibility(View.GONE);
                        editor.putBoolean("watermark_altitude", b);
                        break;
                    case 2:
                        if (b)
                            textView_speed.setVisibility(View.VISIBLE);
                        else
                            textView_speed.setVisibility(View.GONE);
                        editor.putBoolean("watermark_speed", b);
                        break;
                    case 3:
                        editor.putBoolean("watermark_time", b);
                        break;
                    }
                    editor.apply();
                }
            })
            .show();
            break;
            ......
        }
    }
}

3.绘制水印

public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[]data, Camera camera) {
        Log.e(Thread.currentThread().getStackTrace()[2] + "TakePicture", "PictureCallback");
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(ccn, cameraInfo);        
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(bitmap, 0, 0, null);

        //水印
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(bitmap.getWidth() / 30);
        if (sharedPreferences.getBoolean("watermark_geo", true)) {
            canvas.drawText(textView_longitude.getText().toString(), 100, bitmap.getHeight() - 4 * bitmap.getWidth() / 20, paint);
            canvas.drawText(textView_latitude.getText().toString(), 100, bitmap.getHeight() - 3 * bitmap.getWidth() / 20, paint);
        }
        if (sharedPreferences.getBoolean("watermark_altitude", true))
            canvas.drawText(textView_altitude.getText().toString(), 100, bitmap.getHeight() - 2 * bitmap.getWidth() / 20, paint);
        if (sharedPreferences.getBoolean("watermark_speed", true))
            canvas.drawText(textView_speed.getText().toString(), 100, bitmap.getHeight() - bitmap.getWidth() / 20, paint);
        Date date = new Date();
        if (sharedPreferences.getBoolean("watermark_time", true))
            canvas.drawText(SDF1.format(date), 100, bitmap.getHeight() - 15, paint);
        ......
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值