如何从编辑文本中的可滚动内容创建位图图像?

时间:2012-09-25 06:37:47

标签: android bitmap android-edittext

我想捕获图像中的编辑文本内容。但是文本可以滚动。

如何使用编辑文本中的可滚动内容进行捕获?

没有可滚动我使用以下链接来做..

Create Bitmap Image from EditText & its content

请帮我解决问题

1 个答案:

答案 0 :(得分:2)

以下是示例代码

请尽力......

Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
            R.drawable.edittextimage);// get the image same as your EditText

    bitmap = convertToMutable(bitmap);// converting the bitmap to mutable
    Canvas cs = new Canvas(bitmap);

    int h = bitmap.getHeight();
    int w = bitmap.getWidth();
    Paint pt = new Paint();
    pt.setColor(Color.GREEN);
    String iam = "your text that get from the Edit Text";
    cs.drawText(iam, 0, iam.length(), (h / 2) + 10, (w / 2) / 2, pt);
    pt.setColor(Color.RED);
    cs.drawText("this is praki", 0, 13, h / 2, w / 3, pt);

    Save_to_SD(bitmap , path)//save the bitmap in to sdcard

convertToMutable(bitmap)Methode ....

public static Bitmap convertToMutable(Bitmap imgIn) {
    try {
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + "temp.tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = imgIn.getWidth();
        int height = imgIn.getHeight();
        Bitmap.Config type = imgIn.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE,
                0, imgIn.getRowBytes() * height);
        imgIn.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        imgIn.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        imgIn = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        imgIn.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temp file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return imgIn;
}

Save_to_SD(位图,路径)方法....

public static void Save_to_SD(Bitmap bm, String image_name) {

    // String extStorageDirectory =
    // Environment.getExternalStorageDirectory()
    // .toString();
    // String meteoDirectory_path = extStorageDirectory +
    // "/Weather_Belgium";

    OutputStream outStream = null;
    File file = new File(image_name);
    try {
        outStream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();

        Log.i("Hub", "OK, Image Saved to SD");
        Log.i("Hub",
                "height = " + bm.getHeight() + ", width = " + bm.getWidth());

    } catch (FileNotFoundException e) {

        e.printStackTrace();
        Log.i("Hub", "FileNotFoundException: " + e.toString());

    } catch (IOException e) {

        e.printStackTrace();
        Log.i("Hub", "IOException: " + e.toString());
    }

}