图片64位加密

本文介绍了一种图片64位加密的方法,通过使用Base64编码将图片转换为字符串,便于在网络中传输。文章提供了从相册选择图片和通过相机拍摄图片后的加密流程,并针对内存溢出问题给出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目需要图片的64位加密

主要是这个代码:

byte[] encode = Base64.encode(bytes, Base64.DEFAULT);

全部:

/**
 * 图片64位加密 相册
 *
 * @param data
 */
private void encode(Intent data) {
    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Log.d("TAG", "bitmap width: " + bitmap.getWidth() + " height: " + bitmap.getHeight());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();

    //base64 encode
    byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
    encodeString = new String(encode);
}

/**
 * 图片64位加密 相机
 *
 * @param path
 */
private void encode(File path) {
    Bitmap bitmap = BitmapFactory.decodeFile(path.getPath());
    //Log.d("TAG", "bitmap width: " + bitmap.getWidth() + " height: " + bitmap.getHeight());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();

    //base64 encode
    byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
    encodeString = new String(encode);
}
/**
 * 调用系统相机拍照
 */
private void startCamera() {
    Intent intent = new Intent();
    //启动相机的Action
    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    //文件的保存位置
    file = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //设置图片拍摄后保存的位置
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    //启动相机,这里使用有返回结果的启动

    startActivityForResult(intent, PICTURE_FROM_CAMERA_S);
}
相册

tvFile.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        //设置启动相册的Action
        intent.setAction(Intent.ACTION_GET_CONTENT);
        //设置类型
        intent.setType("image/*");
        //启动相册,这里使用有返回结果的启动
        startActivityForResult(intent, PICTURE_FROM_GALLERY_S);
        mPopWindow.dismiss();
    }
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case PICTURE_FROM_CAMERA_S:
                encode(file);//加密
                postSendFileGame(encodeString);
                break;
            case PICTURE_FROM_GALLERY_S:
                encode(data);//加密//加密
                postSendFileGame(encodeString);
                break;
        }
    }
}

这样可能导致内存溢出:解决办法

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(path.getPath() , bitmapOptions);


/**
 * 图片64位加密 相册
 *
 * @param data
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private void encode(Intent data) {
    Bitmap bitmap = null;
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = 4;
    try {
        bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(data.getData()), null , bitmapOptions);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    //Log.d("TAG", "bitmap width: " + bitmap.getWidth() + " height: " + bitmap.getHeight()+"  bitmap.size"+bitmap.getAllocationByteCount());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bytes =baos.toByteArray();

    //base64 encode
    byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
    encodeString = new String(encode);
}

/**
 * 图片64位加密 相机
 *
 * @param path
 */
private void encode(File path) {
    Bitmap bitmap = null;
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = 4;
    bitmap = BitmapFactory.decodeFile(path.getPath() , bitmapOptions);

    //Bitmap bitmap = BitmapFactory.decodeFile(path.getPath());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();

    //base64 encode
    byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
    encodeString = new String(encode);
}








评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大不懂

码字不易,一块也是爱,么么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值