Base64编码和查看图像

时间:2011-12-27 17:36:54

标签: php android bitmap base64

我目前要做的是以下内容:

拍摄照片,将其压缩为PNG,使用Base64进行隐蔽,然后通过PHP将其发送到数据库。这一切都像一个魅力。我也可以解码Base64字符串并完美地显示照片。

然而,问题似乎是当我在浏览器上查看图片时,它的大小与应有的大小不同(或者至少我认为)。

这是我在浏览器中编码和显示的图像: enter image description here

无论手机如何(也在Galaxy SII上测试过),问题仍然存在。你们有什么想法可能会发生什么吗?以下是我的一些想法,但不确定...... 1.压缩图像太多 - 或者压缩图像时会改变一些东西2.Base64编码/解码会弄乱照片而不能正确显示它3.当我将图像放在图像视图中时,它会修改尺寸。

同样,似乎完整的图像没有被发送,因为即使Galaxy sII图像看起来那么小也没有办法。

以下是我使用的代码: 机器人:

// CONVERT:
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        picture.compress(Bitmap.CompressFormat.PNG, 100, bao);
        Log.d(TAG, "AFTER. Height: " + picture.getHeight() + " Width: " + picture.getWidth());
        final byte[] ba = bao.toByteArray();

        //encode to string
        String photoTest = Base64.encodeToString(ba, Base64.DEFAULT);

//Camera on activity for result - save it as a bmp and place in imageview
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            // do something
        }

        if (resultCode == Activity.RESULT_OK) {
            Log.d(TAG, "result ok");

            Bundle b = data.getExtras();
            picture = (Bitmap) b.get("data");
            imageView.setImageBitmap(picture);
        }
    }

ANDROID中的图片视图:

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/icon"/>

PHP:

$image = base64_decode($_POST['image_data']);

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

Base64完全保留二进制数据,不会调整噱头的大小。我猜测问题行是picture.compress(Bitmap.CompressFormat.PNG, 100, bao);,但这不是我的领域。

答案 1 :(得分:1)

我是如何解决的:事实证明,当您像我上面那样保存照片时,它只返回照片的缩略图版本而不是完整尺寸的照片。我必须将EXTRA_OUTPUT添加到我的意图,将其保存到SD卡,然后压缩。如果有人有类似的问题,请给我留言。

答案 2 :(得分:0)

我遇到了同样的问题,这就是我如何解决我的问题,这个方法将打开默认的相机应用程序,将图像保存到临时文件夹并将其转换为Base64 / ByteArray。

1 - 单击“侦听器”以打开相机应用程序

// My ImageButton click Listener
ibt_foto.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String tempDir = Environment.getExternalStorageDirectory() + "/folder/";
        String path = Environment.getExternalStorageDirectory() + "/folder/photo1.jpg";
        prepareDirectory();
        File file = new File(path);
        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }
});

2 - 创建文件夹

private boolean prepareDirectory() {
    try {
        if (makedirs()) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show();
        return false;
    }
}

private boolean makedirs() {

    File tempdir = new File(tempDir);
    if (!tempdir.exists()) {
        tempdir.mkdirs();
    }

    /*
    // Use this if you want to delete older files
    if (tempdir.isDirectory()) {
        File[] files = tempdir.listFiles();
        for (File file : files) {
            if (!file.delete()) {
                System.out.println("Failed to delete " + file);
            }
        }
    }
    */

    return (tempdir.isDirectory());
}

3 - 处理相机应用程序的结果

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    System.gc(); 

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            try {

                // Get image
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 3;
                Bitmap imageBitmap = BitmapFactory.decodeFile(path, options);

                // Converting to ByteArray and Base64
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                boolean validaCompressao = imageBitmap.compress(Bitmap.CompressFormat.JPEG, 75, outputStream);
                byte[] fotoBinario = outputStream.toByteArray();

                String encodedImage = Base64.encodeToString(fotoBinario, Base64.DEFAULT);

                // setting image to my ImageButton
                ibt_foto.setImageBitmap(imageBitmap);

            } catch (Exception e) {
                Toast.makeText(this, "Picture Not taken",Toast.LENGTH_LONG).show();e.printStackTrace();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Picture was not taken 1 ", Toast.LENGTH_SHORT);
        } else {
            Toast.makeText(this, "Picture was not taken 2 ", Toast.LENGTH_SHORT);
        }
    }
}