有没有办法旋转字节数组而不将其解码为Bitmap?
目前在jpeg PictureCallback中我只是将字节数组直接写入文件.但图片是旋转的.我想旋转它们而不解码到位图,希望这将节省我的记忆.
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, o);
int orientation;
if (o.outHeight < o.outWidth) {
orientation = 90;
} else {
orientation = 0;
}
File photo = new File(tmp, "demo.jpeg");
FileOutputStream fos;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(photo);
bos = new BufferedOutputStream(fos);
bos.write(data);
bos.flush();
} catch (IOException e) {
Log.e(TAG, "Failed to save photo", e);
} finally {
IOUtils.closeQuietly(bos);
}
Run Code Online (Sandbox Code Playgroud) 当用户点击按钮时,我有一个正在旋转的图像.但它没有用.
我想看到图像逐渐旋转到90度直到它停止但它没有.单击按钮时,图像必须逐渐旋转90度.
我创建了一个SSCCE来演示这个问题.请使用CrossingPanelSSCE您选择的任何图像替换班级中的图像.只需将图像放入您的images文件夹并命名即可images/railCrossing.JPG.
RotateButtonSSCE
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
public class RotateButtonSSCE extends JPanel implements ActionListener{
private JButton rotate = new JButton("Rotate");
private VisualizationPanelSSCE vis = new VisualizationPanelSSCE();
public RotateButtonSSCE() {
this.setBorder(BorderFactory.createTitledBorder("Rotate Button "));
this.rotate.addActionListener(this);
this.add(rotate);
}
public void actionPerformed(ActionEvent ev) {
vis.rotatetheCrossing();
}
}
Run Code Online (Sandbox Code Playgroud)
CrossingPanelSSCE
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import …Run Code Online (Sandbox Code Playgroud) 我设法旋转图像,180 degrees但希望旋转它90 degrees clockwise可以有人编辑我的代码,以便它做到这一点解释.谢谢.
private void rotateClockwise()
{
if(currentImage != null){
int width = currentImage.getWidth();
int height = currentImage.getHeight();
OFImage newImage = new OFImage(width, height);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
newImage.setPixel( x, height-y-1, currentImage.getPixel(x, y));
}
}
currentImage = newImage;
imagePanel.setImage(currentImage);
frame.pack();
}
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个以0°旋转存储的数组:
0 0 1 0 0
0 0 1 0 0
1 1 1 0 0
0 0 0 0 0
0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
如果我通过,我希望它以良好的近似值返回,例如30°作为参数,它将是这样的:
0 0 0 1 0
1 1 0 1 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
45°会
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
我知道90°旋转的解决方案.但我不认为这会对我有帮助吗? …
我最近看到这个问题,就如何在Java中旋转图像。我直接从该答案中复制/粘贴了它。在实现时,似乎只旋转正方形的图像(即具有相同的大小,宽度和高度)。当我尝试将其用于非正方形图像时,如果可以的话,似乎会切断将其变成矩形的部分。像这样

我该如何解决/解决这个问题?
编辑:我正在使用的代码。另外,我不会有滚动条,因为这将是“游戏”,也不会一直处于全屏状态。
public class Player extends Entity { //Entity has basic values such as (float) x & y values, along with some getters and setters
double theta;
Reticle reticle; //draws a reticle where the cursor was(basically just replaces java.awt.Cursor due to something not neccessary for me to get into)
Sprite currentImage; //basically just a BufferedImage that you can apply aspect ratios to
//constructor
@Override
public void tick() {
//(this line) gets the Reticle from the main-method class …Run Code Online (Sandbox Code Playgroud)