本文整理汇总了Scala中java.awt.image.BufferedImage类的典型用法代码示例。如果您正苦于以下问题:Scala BufferedImage类的具体用法?Scala BufferedImage怎么用?Scala BufferedImage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BufferedImage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Bubble
//设置package包名称以及导入依赖的类
package com.alvinalexander.bubbles
import java.awt.Color
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.awt.GraphicsConfiguration
import java.awt.Transparency
case class Bubble (
var x: Int,
var y: Int,
var lastX: Int,
var lastY: Int,
circleDiameter: Int,
fgColor: Color,
bgColor: Color,
char: Char
)
{
private var image: BufferedImage = null
def drawBubbleFast(g: Graphics, gc: GraphicsConfiguration) {
val g2 = g.asInstanceOf[Graphics2D]
if (image == null) {
println("image was null")
// build the image on the first call
image = gc.createCompatibleImage(circleDiameter+1, circleDiameter+1, Transparency.BITMASK)
val gImg = image.getGraphics.asInstanceOf[Graphics2D]
renderBubble(gImg)
gImg.dispose
}
g2.drawImage(image, x, y, null)
}
// given a Graphics object, render the bubble
def renderBubble(g: Graphics) {
val g2 = g.create.asInstanceOf[Graphics2D]
// draw the circle
g2.setColor(bgColor)
g2.fillOval(0, 0, circleDiameter, circleDiameter)
// draw the character
g2.setFont(CIRCLE_FONT)
g2.setColor(fgColor)
g2.drawString(char.toString, CIRCLE_FONT_PADDING_X, CIRCLE_FONT_PADDING_Y)
g2.dispose
}
}
示例2: MNISTVisualizer
//设置package包名称以及导入依赖的类
package org.dl4scala.examples.feedforward.anomalydetection
import java.awt.{GridLayout, Image}
import java.awt.image.BufferedImage
import javax.swing.{ImageIcon, JFrame, JLabel, JPanel}
import org.nd4j.linalg.api.ndarray.INDArray
import scala.collection.mutable.ArrayBuffer
class MNISTVisualizer(imageScale: Double, digits: ArrayBuffer[INDArray], title: String, gridWidth: Int) {
def this(imageScale: Double, digits: ArrayBuffer[INDArray], title: String) = {
this(imageScale, digits, title, 5)
}
def visualize(): Unit = {
val frame = new JFrame
frame.setTitle(title)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
val panel = new JPanel
panel.setLayout(new GridLayout(0, gridWidth))
val list = getComponents
for (image <- list) {
panel.add(image)
}
frame.add(panel)
frame.setVisible(true)
frame.pack()
}
def getComponents: ArrayBuffer[JLabel] = {
val images = new ArrayBuffer[JLabel]()
for (arr <- digits) {
val bi = new BufferedImage(28, 28, BufferedImage.TYPE_BYTE_GRAY)
for(i <- 0 until 784) {
bi.getRaster.setSample(i % 28, i / 28, 0, (255 * arr.getDouble(i)).asInstanceOf[Int])
}
val orig = new ImageIcon(bi)
val imageScaled = orig.getImage.getScaledInstance((imageScale * 28).asInstanceOf[Int],
(imageScale * 28).asInstanceOf[Int], Image.SCALE_REPLICATE)
val scaled = new ImageIcon(imageScaled)
images.append(new JLabel(scaled))
}
images
}
}
示例3: Barcodes
//设置package包名称以及导入依赖的类
package controllers
import play.api.mvc.{Action, Controller}
class Barcodes extends Controller {
val ImageResolution = 144
// png? ???? Action
def barcode(ean: Long) = Action {
import java.lang.IllegalArgumentException
val MimeType = "image/png"
try {
val imageData = ean13BarCode(ean, MimeType)
Ok(imageData).as(MimeType)
}
catch {
case e: IllegalArgumentException =>
BadRequest("Couldn't generate bar code. Error: " + e.getMessage)
}
}
def ean13BarCode(ean: Long, mimeType: String): Array[Byte] = {
import java.io.ByteArrayOutputStream
import java.awt.image.BufferedImage
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider
import org.krysalis.barcode4j.impl.upcean.EAN13Bean
val output: ByteArrayOutputStream = new ByteArrayOutputStream
val canvas: BitmapCanvasProvider = new BitmapCanvasProvider(
output, mimeType, ImageResolution, BufferedImage.TYPE_BYTE_BINARY, false, 0)
val barcode = new EAN13Bean()
barcode.generateBarcode(canvas, String valueOf ean)
canvas.finish
output.toByteArray
}
}
示例4: OpenCVOps
//设置package包名称以及导入依赖的类
package com.dataoptimo.imgprocessing
import org.opencv.core.Mat
import javax.imageio.ImageIO
import java.io.ByteArrayInputStream
import java.awt.image.DataBufferByte
import org.opencv.core.CvType
import org.opencv.core.Core
import org.opencv.features2d.FeatureDetector
import org.opencv.core.MatOfKeyPoint
import org.opencv.features2d.Features2d
import org.opencv.highgui.Highgui
import java.awt.image.BufferedImage
import java.io.File
object OpenCVOps {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
def imageToMat(byteArray: Array[Byte]): MatSer ={
val bufferedImage = ImageIO.read(new ByteArrayInputStream(byteArray))
val mat = new Mat(bufferedImage.getHeight(), bufferedImage.getWidth(), CvType.CV_8UC3);
val data = bufferedImage.getRaster().getDataBuffer.asInstanceOf[DataBufferByte].getData();
mat.put(0, 0, data);
new MatSer(mat);
}
def matToImage(mat: Mat,file: String): Boolean ={
val imageType = BufferedImage.TYPE_3BYTE_BGR;
val image = new BufferedImage(mat.cols(),mat.rows(), imageType);
val x = image.getRaster.getDataBuffer.asInstanceOf[DataBufferByte].getData
mat.get(0,0,x)
val fileName = new File("file")
ImageIO.write(image, "jpg", fileName)
}
def detectFeatures(mat: Mat)
{
val featureDetector = FeatureDetector.create(FeatureDetector.SIFT)
val matKeyPoint = new MatOfKeyPoint()
featureDetector.detect(mat,matKeyPoint)
println(mat.get(0, 0))
println(matKeyPoint.toList())
//writeToImage(mat,matKeyPoint)
}
def writeToImage(mat: Mat, matKeyPoint: MatOfKeyPoint){
val outImage = new Mat()
Features2d.drawKeypoints(mat, matKeyPoint, outImage)
Highgui.imwrite("myfile.jpg",outImage)
}
}
示例5: Barcodes
//设置package包名称以及导入依赖的类
package controllers
import play.api.mvc.{Action, Controller}
class Barcodes extends Controller{
val ImageResolution = 144
def barcode(ean: Long) = Action{
import java.lang.IllegalArgumentException
val MimeType = "image/png"
try{
val imageData = ean13BarCode(ean, MimeType)
Ok(imageData).as(MimeType)
}catch{
case e:IllegalArgumentException =>
BadRequest("Couldn't generate bar code. Error: " + e.getMessage)
}
}
def ean13BarCode(ean:Long, mimeType: String) : Array[Byte] = {
import java.io.ByteArrayOutputStream
import java.awt.image.BufferedImage
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider
import org.krysalis.barcode4j.impl.upcean.EAN13Bean
val output: ByteArrayOutputStream = new ByteArrayOutputStream
val canvas: BitmapCanvasProvider =
new BitmapCanvasProvider(output, mimeType, ImageResolution,
BufferedImage.TYPE_BYTE_BINARY, false, 0)
val barcode = new EAN13Bean()
barcode.generateBarcode(canvas, String valueOf ean)
canvas.finish
output.toByteArray
}
}
示例6: BrainfuckToKappaLang
//设置package包名称以及导入依赖的类
package com.lthummus.kappalang
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import com.lthummus.kappalang.translator.EmoteTranslator._
object BrainfuckToKappaLang extends App {
val bfCode = scala.io.Source.fromFile(args(0)).getLines.mkString
val widthTokens = args(1).toInt
val heightTokens = args(2).toInt
val dir = args(3)
val out = args(4)
val SymbolMap = Map(
'-' -> ImageIO.read(new File(dir + "BibleThump.png")),
']' -> ImageIO.read(new File(dir + "twitchraid.png")),
'.' -> ImageIO.read(new File(dir + "KAPOW.png")),
'[' -> ImageIO.read(new File(dir + "riPepperonis.png")),
',' -> ImageIO.read(new File(dir + "NotLikeThis.png")),
'+' -> ImageIO.read(new File(dir + "Kappa.png")),
'<' -> ImageIO.read(new File(dir + "RitzMitz.png")),
'>' -> ImageIO.read(new File(dir + "FrankerZ.png"))
)
val NopFile = ImageIO.read(new File(dir + "nop.png"))
if (heightTokens * widthTokens != bfCode.length) {
println("Can't build that with the given dimensions")
println("Dimensions given: " + widthTokens + "x" + heightTokens)
println("Code length: " + bfCode.length)
}
val tokenCords = for {
y <- 0 until heightTokens
x <- 0 until widthTokens
} yield (x * TokenWidth, y * TokenHeight)
val symbols = bfCode.map(SymbolMap.getOrElse(_, NopFile))
val image = new BufferedImage(widthTokens * TokenWidth,
heightTokens * TokenHeight,
BufferedImage.TYPE_INT_RGB)
val graphics = image.getGraphics
for (x <- tokenCords zip symbols) {
graphics.drawImage(x._2, x._1._1, x._1._2, null)
}
ImageIO.write(image, "png", new File(out))
}
示例7: Barcodes
//设置package包名称以及导入依赖的类
package controllers
import play.api.mvc.{Action, Controller}
class Barcodes extends Controller {
val ImageResolution = 144
def barcode(ean: Long) = Action {
import java.lang.IllegalArgumentException
val MimeType = "image/png"
try {
val imageData = ean13BarCode(ean, MimeType)
Ok(imageData).as(MimeType)
}
catch {
case e: IllegalArgumentException =>
BadRequest("Couldn’t generate bar code. Error: " + e.getMessage)
}
}
def ean13BarCode(ean: Long, mimeType: String): Array[Byte] = {
import java.io.ByteArrayOutputStream
import java.awt.image.BufferedImage
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider
import org.krysalis.barcode4j.impl.upcean.EAN13Bean
val output: ByteArrayOutputStream = new ByteArrayOutputStream
val canvas: BitmapCanvasProvider =
new BitmapCanvasProvider(output, mimeType, ImageResolution,
BufferedImage.TYPE_BYTE_BINARY, false, 0)
val barcode = new EAN13Bean()
barcode.generateBarcode(canvas, String valueOf ean)
canvas.finish
output.toByteArray
}
}
示例8: log
//设置package包名称以及导入依赖的类
package de.sciss.imperfect
import java.awt.Color
import java.awt.image.BufferedImage
import java.text.SimpleDateFormat
import java.util.{Date, Locale}
import scala.annotation.elidable
import scala.annotation.elidable.CONFIG
package object hough {
var showLog = true
private[this] val logHeader = new SimpleDateFormat("[d MMM yyyy, HH:mm''ss.SSS] 'imperfect' - ", Locale.US)
final val NominalWidth = 1920
final val NominalHeight = 1080
final val VisibleWidth = 3840
final val VisibleHeight = 540
// final val OffScreenImg = new BufferedImage(VisibleWidth, VisibleHeight, BufferedImage.TYPE_BYTE_GRAY)
final val OffScreenImg = new BufferedImage(VisibleWidth, VisibleHeight, BufferedImage.TYPE_INT_ARGB)
final val OffScreenG = {
val res = OffScreenImg.createGraphics()
res.setColor(Color.black)
res.fillRect(0, 0, VisibleWidth, VisibleHeight)
res
}
@elidable(CONFIG) def log(what: => String): Unit =
if (showLog) Console.out.println(s"${logHeader.format(new Date())}$what")
def warn(s: String): Unit =
Console.err.println(s"Warning: $s")
}
示例9: render
//设置package包名称以及导入依赖的类
package eu.kraml.render
import java.awt.Color
import java.awt.geom.Ellipse2D
import java.awt.image.BufferedImage
import eu.kraml.Main.EventMonitor
import eu.kraml.model.{Circle, HeatMap, PointStyle, Record}
import eu.kraml.render.heatmap._
private[render] trait RecordRenderer {
def render(overlay: BufferedImage, coordinateConverter: CoordinateConverter, records: List[Record])
(implicit progress: EventMonitor): Unit
}
private[render] class CircleRenderer(private val diameter: Int, private val color: Color) extends RecordRenderer {
private val radius = diameter/2.0
override def render(overlay: BufferedImage, coordinateConverter: CoordinateConverter, records: List[Record])
(implicit progress: EventMonitor): Unit = {
val awtG = overlay.createGraphics()
awtG.setColor(color)
val process = progress.startProcess("rendering point")
process.setMaxProgressValue(records.size)
records.foreach( r => {
val center = coordinateConverter.toCanvasCoords(r.coordinate)
val shape = new Ellipse2D.Double(center.x-radius, center.y-radius, diameter, diameter)
awtG.fill(shape)
process << 1
})
awtG.dispose()
process.indicateCompletion()
}
}
private[render] object RecordRenderer {
def getRenderer(style: PointStyle): RecordRenderer =
style match {
case Circle(d, c) => new CircleRenderer(d, c.toAWT)
case HeatMap() => new TiledRenderer(DefaultStyle)
case _ => throw new IllegalArgumentException(s"unknown point style $style")
}
}
示例10: getImageType
//设置package包名称以及导入依赖的类
package org.aj.awslambda
import java.awt.image.BufferedImage
import java.io.IOException
import java.net.URLDecoder
import com.amazonaws.services.lambda.runtime.events.S3Event
import java.util.regex.Pattern
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
private def getImageType(srcKey: String): Option[String] = {
val matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey)
matcher.matches() match {
case true => {
//get source image type and validate
val imageType = matcher.group(1)
imageTypes.get(imageType) match {
case Some(_) => Some(imageType)
case _ => None
}
}
case _ => None
}
}
private def decodeS3Key(key: String): String = URLDecoder.decode(key.replace("+", " "), "utf-8")
private def process(srcBucket: String, srcKey: String, imageType: String, dstBucket: String): Future[List[Url]] = {
//get source image with it's sizes
val sourceImage: (BufferedImage, Url) = getImage(srcBucket, srcKey)
//re-size
val original: Future[Url] = process(sourceImage._1, sourceImage._2, imageType, dstBucket, srcKey)
val resized: List[Option[Future[Url]]] = sizes.map { size =>
if (sourceImage._2.width != size) Some(process(sourceImage._1, size, imageType, dstBucket, srcKey))
else None
}
//return
Future.sequence(original :: resized.flatten)
}
}
示例11: MyTestSuite
//设置package包名称以及导入依赖的类
package scalaview
import org.scalatest._
import Utils._
class MyTestSuite extends FunSuite {
test("1+2=3") {
assert(1 + 2 === 3)
}
test("thin(1) a finite stream") {
val s1 = (1 to 10).toStream
val s2 = thinStream(s1, 1)
assert(s2.toList === s1.toList)
}
test("thin(2) a finite stream") {
val s1 = (1 to 10).toStream
val s2 = thinStream(s1, 2)
assert(s2.toList === List(2, 4, 6, 8, 10))
}
test("thin(3) a finite stream") {
val s1 = (1 to 10).toStream
val s2 = thinStream(s1, 3)
assert(s2.toList === List(3, 6, 9))
}
test("thin an infinite stream") {
val s1 = Stream.iterate(1L)(_ + 1)
val s2 = Stream.iterate(10000L)(_ + 10000)
val s3 = thinStream(s1, 10000)
assert(s2.take(1000).toList === s3.take(1000).toList)
}
test("rescale an image") {
import java.awt.image.BufferedImage
val im1 = new BufferedImage(100, 200, BufferedImage.TYPE_BYTE_GRAY)
val im2 = biResize(im1, 1000, 2000)
assert(im2.getWidth === 1000)
assert(im2.getHeight === 2000)
}
}
示例12: JavaFontConversion
//设置package包名称以及导入依赖的类
package org.nspl
import java.awt.{Font => JFont}
object JavaFontConversion {
implicit def font2font(myFont: Font): JFont = myFont match {
case Monospace => new JFont(JFont.MONOSPACED, JFont.PLAIN, Monospace.size)
case NamedFont(name, size) => new JFont(name, JFont.PLAIN, size)
}
}
object AwtGlyphMeasurer extends GlyphMeasurer[Font#F] {
import JavaFontConversion._
import java.awt.image.BufferedImage
val bimage = new BufferedImage(50, 50, BufferedImage.TYPE_BYTE_BINARY)
val g2d = bimage.createGraphics();
val frc = g2d.getFontRenderContext
val abc =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQRSTUVWXYZ0123456789%,./][()]"
def advance(s: Char, f: Font#F): Double =
font2font(f).getStringBounds(s.toString, frc).getWidth
def lineMetrics(f: Font#F): LineMetrics = {
val lm = font2font(f).getLineMetrics(abc, frc)
LineMetrics(lm.getAscent, lm.getDescent, lm.getLeading)
}
}
示例13: IconService
//设置package包名称以及导入依赖的类
package ylabs.play.common.services
import java.awt.Color
import java.awt.image.{ WritableRaster, BufferedImage }
import scala.util.Random
class IconService {
def createImageWithBackground(image: BufferedImage, palette: List[Color]): BufferedImage = {
val index = new Random().nextInt(palette.length)
val backColor = palette.slice(index, index + 1).head
val raster = image.copyData(null)
val copy = new BufferedImage(image.getColorModel, raster, image.isAlphaPremultiplied, null)
for {
i ? 0 until copy.getWidth
j ? 0 until copy.getHeight
} yield {
new Color(copy.getRGB(i, j), true) match {
case c if c.getAlpha == 0 ?
copy.setRGB(i, j, backColor.getRGB)
case _ ?
}
}
copy
}
}
示例14: IconServiceTest
//设置package包名称以及导入依赖的类
package ylabs.play.common.services
import java.awt.Color
import java.awt.image.BufferedImage
import ylabs.play.common.test.{ MyPlaySpec, OneAppPerTestWithOverrides }
class IconServiceTest extends MyPlaySpec with OneAppPerTestWithOverrides {
"test background" in {
val height = 30
val width = 30
val image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
for {
i ? 0 until height
j ? 0 until width
} yield {
val color = new Color(0, 0, 0, i % 2)
image.setRGB(i, j, color.getRGB)
}
val service = new IconService
val newImage = service.createImageWithBackground(image, List(Color.red, Color.blue, Color.green))
for {
i ? 0 until height
j ? 0 until width
} yield {
val origColor = new Color(0, 0, 0, i % 2)
image.getRGB(i, j) shouldBe origColor.getRGB
if (i % 2 == 0) newImage.getRGB(i, j) should (equal(Color.red.getRGB) or equal(Color.blue.getRGB) or equal(Color.green.getRGB))
else newImage.getRGB(i, j) shouldBe origColor.getRGB
}
}
}
示例15: Barcodes
//设置package包名称以及导入依赖的类
package controllers
import play.api.mvc.{Action, Controller}
class Barcodes extends Controller {
val ImageResolution = 144
def barcode(ean: Long) = Action {
import java.lang.IllegalArgumentException
val MimeType = "image/png"
try {
val imageData = ean13BarCode(ean, MimeType)
Ok(imageData).as(MimeType)
}
catch {
case e: IllegalArgumentException =>
BadRequest("Couldn’t generate bar code. Error: " + e.getMessage)
}
}
def ean13BarCode(ean: Long, mimeType: String): Array[Byte] = {
import java.io.ByteArrayOutputStream
import java.awt.image.BufferedImage
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider
import org.krysalis.barcode4j.impl.upcean.EAN13Bean
val output: ByteArrayOutputStream = new ByteArrayOutputStream
val canvas: BitmapCanvasProvider =
new BitmapCanvasProvider(output, mimeType, ImageResolution,
BufferedImage.TYPE_BYTE_BINARY, false, 0)
val barcode = new EAN13Bean()
barcode.generateBarcode(canvas, String valueOf ean)
canvas.finish
output.toByteArray
}
}