0

我想要做的是从键盘接收一个字符,例如'a'并水平翻转该字母并将其打印出来。我到处搜索翻转的图像,但似乎没有任何方法可以做到这一点。谁能帮我?我正在尝试在 Java 中执行此操作。提前非常感谢,如果您需要更多详细信息,请在评论部分询问我。

ps 我无法向您显示所需的输出,因为我在网上找不到任何翻转的字符。这个概念与图像水平翻转相同,但我想对字符进行此操作。

4

3 回答 3

4

您可以尝试创建 a BufferedImage,获取它Graphics2D并将其设置AffineTransformAffineTransform.scale(-1, 1),然后使用Graphics2D'sdrawString方法。您将无法将其打印到控制台-我认为您的建议根本不可能打印到控制台-但该技术会为您生成图像。

于 2012-01-30T19:16:13.340 回答
1

使用图像实现;不确定是否有办法翻转角色本身。

import java.io.*;

public class RotateAndFlip {


    static JPEGImage rotate90(JPEGImage inputImage) {
    JPEGImage outputImage = new JPEGImage(inputImage.getHeight(),
                          inputImage.getWidth());

    // Code to make outputImage into an image that is a 
    // 90 degree rotation of inputImage

    return outputImage;
    }
    static JPEGImage rotate180(JPEGImage inputImage) {
    return rotate90(rotate90(inputImage));
    }

    static JPEGImage rotate270(JPEGImage inputImage) {
    return rotate90(rotate90(rotate90(inputImage)));
    }

    static JPEGImage flipHorizontal(JPEGImage inputImage) {
    JPEGImage outputImage = new JPEGImage(inputImage.getWidth(),
                          inputImage.getHeight());

    // Code to make outputImage into an image that is a 
    // horizontal flip of inputImage

    return outputImage;
    }

    static JPEGImage flipVertical(JPEGImage inputImage) {
    return rotate90(flipHorizontal(rotate270(inputImage)));
    }

    public static void main (String args[]) {
    /* Check that the user has provided the right number of arguments */
    if (args.length != 1) {
        System.out.println("Usage: java JPEGCopy <source JPEG file> " + 
                   "<target JPEG file>");
        System.exit(1);
    }

    /* Create an empty image. We will read an image from a file into
       this object */
    JPEGImage imageOne = new JPEGImage();

    /* Try to open the file. This may cause an exception if the name 
       given is not a valid JPEG file so we need to catch the exceptions */
    try {
        imageOne.read(args[0]);
    } catch (Exception e) {
        /* An exception has been thrown. This is usually because the file
           either does not exist, or is not a JPEG image */
        System.out.println("Error reading file " + args[0]);
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Make a new image to store the results in */
    JPEGImage imageTwo = new JPEGImage();


    /* Rotate by 90 degrees and write to a file */
    imageTwo = rotate90(imageOne);
    try {
        imageTwo.write("rotate90.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate90.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Rotate by 180 degrees and write to a file */
    imageTwo = rotate180(imageOne);
    try {
        imageTwo.write("rotate180.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate180.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Rotate by -90 degrees and write to a file */
    imageTwo = rotate270(imageOne);
    try {
        imageTwo.write("rotate270.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate270.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Flip horizontally and write to a file */
    imageTwo = flipHorizontal(imageOne);
    try {
        imageTwo.write("flipHorizontal.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file flipHorizontal.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Flip vertically and write to a file */
    imageTwo = flipVertical(imageOne);
    try {
        imageTwo.write("flipVertical.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file flipVertical.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    }
}

这里:http ://www.cs.nott.ac.uk/~smx/IVIPracticals/exercise1.html

于 2012-01-30T19:30:05.530 回答
0

不确定您要完成什么。我认为最简单的方法是获取所有字母和符号的一组图像,使用任何传统的图形软件(PaintShopPro、PhotoShop 等)翻转它们——将图像保存为一堆单独的 GIF 或PNG 文件,然后在需要时检索相应的文件。

如果你真的想在 Java 程序中进行翻转,我认为关键是:

  1. 创建一个 BufferedImage 对象

  2. 对其调用 createGraphics 以获取 Graphics 对象。

  3. 使用 drawString 将一个或多个字符写入此 Graphics 对象

  4. 然后,您可以使用 getRGB 和 setRGB 随意操作图像。基本上,您希望从上到中遍历图像,将上半部分的像素交换到下半部分。

我想如果您希望它与任何字体、多种尺寸等一起使用,您必须在程序中进行。

更新

根据您在 1 月 30 日的评论,您的意思似乎是您不想自己生成反转图像,而是想在 Unicode 字符集中找到反转图像。恐怕,据我所知,Unicode 并不包含每个字符的反向图像。它包括一些本身就是符号并且具有特定含义的符号。例如,Unicode 在 U+2203 处有一个向后大写的 E,但如果您查看其他具有附近 Unicode 代码点的字符,您会发现它们是各种数学符号。向后的 E 是“在那里”,因为它是一个数学符号,意思是“存在”,而不是因为 Unicode 的发明者给出了每个字符的镜像。

我当然没有记住完整的 Unicode 代码集,所以我不能发誓那里没有这样的字符集,但我真的很怀疑。Unicode 的重点应该是提供一种表示世界上所有字母的方法和一组强大的常用符号。他们非常刻意地没有为斜体和粗体或不同大小的内容包含单独的代码点,因为这些事情应该通过选择不同的字体来处理,而不是一组不同的 Unicode 代码点。因此,如果他们添加每个符号的镜像,那将是非常令人惊讶的。这将与 Unicode 的理念相悖。如果他们这样做了,那为什么不颠倒版本呢?为什么不旋转 90 度?等等等等。

所以我认为你的问题的简短回答是,它不能完成。没有这样的 Unicode 字符。

于 2012-01-30T19:17:30.437 回答