ReactNative-Android给当前应用添加水印(转)

使用如何给ReactNative所有界面添加水印效果
/**
 * zjm
 * 2019/2/19
 * 4:32 PM
 */
public class WaterMarkUtil {
    public static String mWaterMarkDesc;

    /**
     * 显示水印布局
     *
     * @param activity
     */
    public static boolean showWatermarkView(final Activity activity) {
        final ViewGroup rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
        View framView = LayoutInflater.from(activity).inflate(R.layout.layout_watermark, null);
       
        //可对水印布局进行初始化操作
        rootView.addView(framView);
        return true;
    }

}
使用说明:

1、layout_watermark.xml 为水印布局文件,可自定义为任何布局,在其跟布局添加 android:alpha 可设置水印的透明度

android:alpha="0.1"
2、在自己的BaseActivity的onCreate方法中添加以下代码,即可为所有Activity添加水印。

WaterMarkUtil.showWatermarkView(this);
如下图使用ReactNative开发界面

https://img-blog.csdnimg.cn/20190221171718972.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lUX2x1bnRhbg==,size_16,color_FFFFFF,t_70

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用 Android 相机 API 来实现拍照并添加地址水印。以下是一个简单的示例代码,展示了如何实现这个功能: ```java import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.media.ExifInterface; import android.media.Image; import android.media.ImageReader; import android.os.Bundle; import android.os.Environment; import android.util.Log; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Locale; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final int IMAGE_WIDTH = 1920; // 图片宽度 private static final int IMAGE_HEIGHT = 1080; // 图片高度 private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireLatestImage(); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); // 将图片保存到文件 File file = saveImage(bytes); if (file != null) { try { // 读取图片的经纬度信息 ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath()); float[] latLong = new float[2]; if (exifInterface.getLatLong(latLong)) { // 将经纬度换为地址 String address = getAddressFromLocation(latLong[0], latLong[1]); // 在图片上绘制地址水印 Bitmap bitmap = drawTextOnBitmap(bytes, address); // 保存加上水印的图片 saveImage(bitmap); } } catch (IOException e) { e.printStackTrace(); } } image.close(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 创建一个 ImageReader,监听相机拍照的图像数据 ImageReader imageReader = ImageReader.newInstance(IMAGE_WIDTH, IMAGE_HEIGHT, ImageFormat.JPEG, 1); imageReader.setOnImageAvailableListener(mOnImageAvailableListener, null); // 打开相机进行拍照 CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { String cameraId = cameraManager.getCameraIdList()[0]; cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() { @Override public void onOpened(@NonNull CameraDevice camera) { try { CaptureRequest.Builder captureRequestBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureRequestBuilder.addTarget(imageReader.getSurface()); captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); camera.createCaptureSession(Collections.singletonList(imageReader.getSurface()), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { try { session.capture(captureRequestBuilder.build(), null, null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { Log.e(TAG, "Failed to configure camera capture session"); } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onDisconnected(@NonNull CameraDevice camera) { camera.close(); } @Override public void onError(@NonNull CameraDevice camera, int error) { camera.close(); } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private File saveImage(byte[] bytes) { File file = null; try { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); file = new File(storageDir, "IMG_" + timeStamp + ".jpg"); FileOutputStream output = new FileOutputStream(file); output.write(bytes); output.close(); } catch (IOException e) { e.printStackTrace(); } return file; } private String getAddressFromLocation(double latitude, double longitude) { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); String addressStr = ""; try { List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); addressStr = address.getAddressLine(0); } } catch (IOException e) { e.printStackTrace(); } return addressStr; } private Bitmap drawTextOnBitmap(byte[] bytes, String text) { Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length).copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.WHITE); paint.setTextSize(50); float x = 50; float y = 50; canvas.drawText(text, x, y, paint); return bitmap; } private void saveImage(Bitmap bitmap) { File file = null; try { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); file = new File(storageDir, "IMG_" + timeStamp + ".jpg"); FileOutputStream output = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output); output.close(); } catch (IOException e) { e.printStackTrace(); } if (file != null) { // 在这里可以将图片路径传递给其他组件或进行其他操作 Log.d(TAG, "保存图片成功: " + file.getAbsolutePath()); } } } ``` 这个示例代码使用了相机 API 进行拍照,并通过 ImageReader 获取图像数据。然后,它通过 ExifInterface 读取图片的经纬度信息,并使用 Geocoder 将经纬度换为地址。最后,它在图片上绘制地址水印,并保存加上水印的图片。 请注意,这只是一个简单的示例,您可能需要根据自己的需求进行修改和优化。同时,为了使用位置信息,您需要在应用AndroidManifest.xml 文件中添加相应的权限声明和特殊权限请求。 希望这可以帮助到您!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值