Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
XChart
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Brady James Garvin
XChart
Commits
27bc77fc
Commit
27bc77fc
authored
12 years ago
by
timmolter
Browse files
Options
Downloads
Patches
Plain Diff
added jpeg saving
parent
8d5d8ae7
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/com/xeiam/xchart/BitmapEncoder.java
+45
-7
45 additions, 7 deletions
src/main/java/com/xeiam/xchart/BitmapEncoder.java
with
45 additions
and
7 deletions
src/main/java/com/xeiam/xchart/BitmapEncoder.java
+
45
−
7
View file @
27bc77fc
...
@@ -17,13 +17,22 @@ package com.xeiam.xchart;
...
@@ -17,13 +17,22 @@ package com.xeiam.xchart;
import
java.awt.Graphics2D
;
import
java.awt.Graphics2D
;
import
java.awt.image.BufferedImage
;
import
java.awt.image.BufferedImage
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.Iterator
;
import
javax.imageio.IIOImage
;
import
javax.imageio.ImageIO
;
import
javax.imageio.ImageIO
;
import
javax.imageio.ImageWriteParam
;
import
javax.imageio.ImageWriter
;
import
javax.imageio.stream.FileImageOutputStream
;
/**
/**
* A helper class with static methods for saving Charts as bitmaps
*
* @author timmolter
* @author timmolter
*/
*/
public
class
BitmapEncoder
{
public
class
BitmapEncoder
{
...
@@ -36,22 +45,51 @@ public class BitmapEncoder {
...
@@ -36,22 +45,51 @@ public class BitmapEncoder {
}
}
/**
/**
* Save
s
a
c
hart as a PNG file
* Save a
C
hart as a PNG file
*
*
* @param chart
* @param chart
* @param
pF
ileName
* @param
f
ileName
* @throws IOException
* @throws IOException
*/
*/
public
static
void
savePNG
(
Chart
chart
,
String
pF
ileName
)
throws
IOException
{
public
static
void
savePNG
(
Chart
chart
,
String
f
ileName
)
throws
IOException
{
BufferedImage
lB
ufferedImage
=
new
BufferedImage
(
chart
.
width
,
chart
.
height
,
BufferedImage
.
TYPE_INT_RGB
);
BufferedImage
b
ufferedImage
=
new
BufferedImage
(
chart
.
width
,
chart
.
height
,
BufferedImage
.
TYPE_INT_RGB
);
Graphics2D
lGraphics2D
=
lB
ufferedImage
.
createGraphics
();
Graphics2D
lGraphics2D
=
b
ufferedImage
.
createGraphics
();
chart
.
paint
(
lGraphics2D
);
chart
.
paint
(
lGraphics2D
);
// Save chart as PNG
// Save chart as PNG
OutputStream
out
=
new
FileOutputStream
(
pF
ileName
);
OutputStream
out
=
new
FileOutputStream
(
f
ileName
);
ImageIO
.
write
(
lB
ufferedImage
,
"png"
,
out
);
ImageIO
.
write
(
b
ufferedImage
,
"png"
,
out
);
out
.
close
();
out
.
close
();
}
}
/**
* Save a Chart as a JPEG file
*
* @param chart
* @param fileName
* @param quality - // a float between 0 and 1 (1 = maximum quality)
* @throws FileNotFoundException
* @throws IOException
*/
public
static
void
saveJPG
(
Chart
chart
,
String
fileName
,
float
quality
)
throws
FileNotFoundException
,
IOException
{
BufferedImage
bufferedImage
=
new
BufferedImage
(
chart
.
width
,
chart
.
height
,
BufferedImage
.
TYPE_INT_RGB
);
Graphics2D
lGraphics2D
=
bufferedImage
.
createGraphics
();
chart
.
paint
(
lGraphics2D
);
Iterator
<
ImageWriter
>
iter
=
ImageIO
.
getImageWritersByFormatName
(
"jpeg"
);
ImageWriter
writer
=
iter
.
next
();
// instantiate an ImageWriteParam object with default compression options
ImageWriteParam
iwp
=
writer
.
getDefaultWriteParam
();
iwp
.
setCompressionMode
(
ImageWriteParam
.
MODE_EXPLICIT
);
iwp
.
setCompressionQuality
(
quality
);
File
file
=
new
File
(
fileName
);
FileImageOutputStream
output
=
new
FileImageOutputStream
(
file
);
writer
.
setOutput
(
output
);
IIOImage
image
=
new
IIOImage
(
bufferedImage
,
null
,
null
);
writer
.
write
(
null
,
image
,
iwp
);
writer
.
dispose
();
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment