Skip to content

Commit

Permalink
Fixing ViewImage
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Apr 13, 2023
1 parent 810d1f5 commit 456ec4b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 62 deletions.
20 changes: 20 additions & 0 deletions src/main/java/org/luwrain/app/viewer/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public App(String arg)
final ViewPdf viewPdf = createPdfView(file);
viewPdf.show();
setAppName(file.getName());
} else
if (file != null && file.getName().toUpperCase().endsWith(".JPG"))
{
final ViewImage viewImage = createImageView(file);
viewImage.show();
setAppName(file.getName());
} else
setAppName(getStrings().appName());
this.mainLayout = new MainLayout(this);
Expand All @@ -81,4 +87,18 @@ private ViewPdf createPdfView(File file) throws IOException
@Override void announceZoomOut() {}
};
}

private ViewImage createImageView(File file) throws IOException
{
return new ViewImage(getLuwrain(), file){
@Override void inaccessible() { getLuwrain().playSound(Sounds.EVENT_NOT_PROCESSED); }
@Override void announcePage(int pageNum, int pageCount) { message(getStrings().pdfPage(String.valueOf(pageNum), String.valueOf(pageCount)), Luwrain.MessageType.OK); }
@Override void announceMoveLeft() {}
@Override void announceMoveRight() {}
@Override void announceMoveUp() {}
@Override void announceMoveDown() {}
@Override void announceZoomIn() {}
@Override void announceZoomOut() {}
};
}
}
93 changes: 31 additions & 62 deletions src/main/java/org/luwrain/app/viewer/ViewImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,44 @@
import javafx.concurrent.Worker.State;
import javafx.scene.input.KeyEvent;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import org.luwrain.core.*;
import org.luwrain.core.events.*;
import org.luwrain.graphical.*;

import static org.luwrain.graphical.FxThread.*;

abstract class ViewImage
{
static final String LOG_COMPONENT = "pdf";
static final String
LOG_COMPONENT = "image";
static private final float SCALE_STEP = 0.2f;
static private final double OFFSET_STEP = 200.0;

private final Luwrain luwrain;
private ResizableCanvas canvas = null;
private final PDDocument doc;
private final PDFRenderer rend;
private Image image = null;

private int pageNum = 0;
private float scale = 1;
private double offsetX = 0;
private double offsetY = 0;
private double
offsetX = 0,
offsetY = 0;

ViewImage(Luwrain luwrain)
ViewImage(Luwrain luwrain, File file) throws IOException
{
NullCheck.notNull(luwrain, "luwrain");
this.luwrain = luwrain;
this.doc = null;//PDDocument.load(file);
this.rend = new PDFRenderer(doc);
Log.debug(LOG_COMPONENT, "PDF renderer created");
try (final InputStream is = new FileInputStream(file)) {
this.image = new Image(is);
}
}

abstract void inaccessible();
abstract void inaccessible();
abstract void announcePage(int pageNum, int pageCount);
abstract void announceMoveLeft();
abstract void announceMoveRight();
abstract void announceMoveRight();
abstract void announceMoveUp();
abstract void announceMoveDown();
abstract void announceMoveDown();
abstract void announceZoomIn();
abstract void announceZoomOut();



public void show()
{
Expand All @@ -83,20 +78,19 @@ public void show()
this.canvas.setOnKeyPressed((event)->onKey(event));
this.canvas.setVisible(true);
this.canvas.requestFocus();
drawInitial();
draw();
return canvas;
}
catch(Throwable e)
{
Log.error(LOG_COMPONENT, "unable to initialize the PDF preview:" + e.getClass().getName() + ":" + e.getMessage());
Log.error(LOG_COMPONENT, "unable to initialize the image preview: " + e.getClass().getName() + ": " + e.getMessage());
e.printStackTrace();
this.canvas = null;
return null;
}
});
}


/*
private void moveRight()
{
Expand Down Expand Up @@ -147,23 +141,17 @@ private void zoomOut()
}
*/

private void drawInitial()
{
this.image = makeImage(pageNum, 1);
draw();
}

public void close()
{
FxThread.runSync(()->{
runSync(()->{
// interaction.closeCanvas(this.canvas);
// interaction.disableGraphicalMode();
});
}

private void draw()
{
FxThread.ensure();
ensure();
if (image == null || canvas == null)
return;
final double imageWidth = image.getWidth();
Expand All @@ -180,27 +168,9 @@ private void draw()
horizFrag.to, vertFrag.to, horizFrag.size, vertFrag.size);
}

private Image makeImage(int pageNum, float scale)
{
FxThread.ensure();
final BufferedImage pageImage;
try {
pageImage = rend.renderImage(pageNum, scale);
}
catch (IOException e)
{
Log.error(LOG_COMPONENT, "unable to render a PDf page:" + e.getClass().getName() + ":" + e.getMessage());
return null;
}
final Image image = null;//SwingFXUtils.toFXImage(pageImage, null);
Log.debug(LOG_COMPONENT, "image " + String.format("%.2f", image.getWidth()) + "x" + String.format("%.2f", image.getHeight()));
return image;
}

private void onKey(KeyEvent event)
{
NullCheck.notNull(event, "event");
FxThread.ensure();
ensure();
switch(event.getCode())
{
case ESCAPE:
Expand Down Expand Up @@ -243,19 +213,6 @@ private void onKey(KeyEvent event)
}
}

static private final class Fragment
{
final double from;
final double to;
final double size;
Fragment(double from, double to, double size)
{
this.from = from;
this.to = to;
this.size = size;
}
}

Fragment calcFragment(double imageSize, double screenSize, double offset)
{
if (imageSize < screenSize)
Expand All @@ -269,4 +226,16 @@ static double matchingScale(double imageWidth, double imageHeight, double screen
final double vertScale = screenHeight / imageHeight;
return Math.min(horizScale, vertScale);
}

static private final class Fragment
{
final double from, to, size;
Fragment(double from, double to, double size)
{
this.from = from;
this.to = to;
this.size = size;
}
}

}

0 comments on commit 456ec4b

Please sign in to comment.