Correcting default sizing of bitmaps, code review adjustments

This commit is contained in:
Arnold Szabo 2018-11-12 23:11:44 +02:00
parent d159050eeb
commit 78ef90f61c
3 changed files with 42 additions and 23 deletions

View File

@ -70,7 +70,6 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder {
private static final String ATTR_REGION = "region"; private static final String ATTR_REGION = "region";
private static final String ATTR_IMAGE = "backgroundImage"; private static final String ATTR_IMAGE = "backgroundImage";
private static final Pattern CLOCK_TIME = private static final Pattern CLOCK_TIME =
Pattern.compile("^([0-9][0-9]+):([0-9][0-9]):([0-9][0-9])" Pattern.compile("^([0-9][0-9]+):([0-9][0-9]):([0-9][0-9])"
+ "(?:(\\.[0-9]+)|:([0-9][0-9])(?:\\.([0-9]+))?)?$"); + "(?:(\\.[0-9]+)|:([0-9][0-9])(?:\\.([0-9]+))?)?$");
@ -130,7 +129,7 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder {
Log.i(TAG, "Ignoring unsupported tag: " + xmlParser.getName()); Log.i(TAG, "Ignoring unsupported tag: " + xmlParser.getName());
unsupportedNodeDepth++; unsupportedNodeDepth++;
} else if (TtmlNode.TAG_HEAD.equals(name)) { } else if (TtmlNode.TAG_HEAD.equals(name)) {
parseHeader(xmlParser, globalStyles, regionMap, cellResolution, imageMap); parseHeader(xmlParser, globalStyles, cellResolution, regionMap, imageMap);
} else { } else {
try { try {
TtmlNode node = parseNode(xmlParser, parent, regionMap, frameAndTickRate); TtmlNode node = parseNode(xmlParser, parent, regionMap, frameAndTickRate);
@ -232,8 +231,8 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder {
private Map<String, TtmlStyle> parseHeader( private Map<String, TtmlStyle> parseHeader(
XmlPullParser xmlParser, XmlPullParser xmlParser,
Map<String, TtmlStyle> globalStyles, Map<String, TtmlStyle> globalStyles,
Map<String, TtmlRegion> globalRegions,
CellResolution cellResolution, CellResolution cellResolution,
Map<String, TtmlRegion> globalRegions,
Map<String, String> imageMap) Map<String, String> imageMap)
throws IOException, XmlPullParserException { throws IOException, XmlPullParserException {
do { do {
@ -255,23 +254,21 @@ public final class TtmlDecoder extends SimpleSubtitleDecoder {
globalRegions.put(ttmlRegion.id, ttmlRegion); globalRegions.put(ttmlRegion.id, ttmlRegion);
} }
} else if(XmlPullParserUtil.isStartTag(xmlParser, TtmlNode.TAG_METADATA)){ } else if(XmlPullParserUtil.isStartTag(xmlParser, TtmlNode.TAG_METADATA)){
parseMetaData(xmlParser, imageMap); parseMetadata(xmlParser, imageMap);
} }
} while (!XmlPullParserUtil.isEndTag(xmlParser, TtmlNode.TAG_HEAD)); } while (!XmlPullParserUtil.isEndTag(xmlParser, TtmlNode.TAG_HEAD));
return globalStyles; return globalStyles;
} }
public void parseMetaData(XmlPullParser xmlParser, Map<String, String> imageMap) throws IOException, XmlPullParserException { private void parseMetadata(XmlPullParser xmlParser, Map<String, String> imageMap) throws IOException, XmlPullParserException {
do { do {
xmlParser.next(); xmlParser.next();
if (XmlPullParserUtil.isStartTag(xmlParser, TtmlNode.TAG_SMPTE_IMAGE)) { if (XmlPullParserUtil.isStartTag(xmlParser, TtmlNode.TAG_SMPTE_IMAGE)) {
for (int i = 0; i < xmlParser.getAttributeCount(); i++) { String id = XmlPullParserUtil.getAttributeValue(xmlParser, "id");
String id = XmlPullParserUtil.getAttributeValue(xmlParser, "id"); if (id != null) {
if(id != null){ String base64 = xmlParser.nextText();
String base64 = xmlParser.nextText(); imageMap.put(id, base64);
imageMap.put(id, base64);
}
} }
} }
} while (!XmlPullParserUtil.isEndTag(xmlParser, TtmlNode.TAG_METADATA)); } while (!XmlPullParserUtil.isEndTag(xmlParser, TtmlNode.TAG_METADATA));

View File

@ -183,18 +183,24 @@ import java.util.TreeSet;
public List<Cue> getCues(long timeUs, Map<String, TtmlStyle> globalStyles, public List<Cue> getCues(long timeUs, Map<String, TtmlStyle> globalStyles,
Map<String, TtmlRegion> regionMap, Map<String, String> imageMap) { Map<String, TtmlRegion> regionMap, Map<String, String> imageMap) {
TreeMap<String, SpannableStringBuilder> regionOutputs = new TreeMap<>(); TreeMap<String, SpannableStringBuilder> regionTextOutputs = new TreeMap<>();
List<Pair<String, String>> regionImageList = new ArrayList<>(); List<Pair<String, String>> regionImageOutputs = new ArrayList<>();
traverseForText(timeUs, false, regionId, regionOutputs); traverseForText(timeUs, false, regionId, regionTextOutputs);
traverseForStyle(timeUs, globalStyles, regionOutputs); traverseForStyle(timeUs, globalStyles, regionTextOutputs);
traverseForImage(timeUs, regionId, regionImageList); traverseForImage(timeUs, regionId, regionImageOutputs);
List<Cue> cues = new ArrayList<>(); List<Cue> cues = new ArrayList<>();
// Create image based cues // Create image based cues
for (Pair<String, String> regionImagePair : regionImageList) { for (Pair<String, String> regionImagePair : regionImageOutputs) {
String base64 = imageMap.get(regionImagePair.second); String base64 = imageMap.get(regionImagePair.second);
if (base64 == null) {
// Image ref points to invalid image, do nothing
continue;
}
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT); byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
TtmlRegion region = regionMap.get(regionImagePair.first); TtmlRegion region = regionMap.get(regionImagePair.first);
@ -206,13 +212,13 @@ import java.util.TreeSet;
region.line, region.line,
region.lineAnchor, region.lineAnchor,
region.width, region.width,
Cue.DIMEN_UNSET /* height= */ Cue.DIMEN_UNSET
) )
); );
} }
// Create text based cues // Create text based cues
for (Entry<String, SpannableStringBuilder> entry : regionOutputs.entrySet()) { for (Entry<String, SpannableStringBuilder> entry : regionTextOutputs.entrySet()) {
TtmlRegion region = regionMap.get(entry.getKey()); TtmlRegion region = regionMap.get(entry.getKey());
cues.add( cues.add(
new Cue( new Cue(

View File

@ -355,8 +355,12 @@ import com.google.android.exoplayer2.util.Util;
} }
private void setupBitmapLayout() { private void setupBitmapLayout() {
int parentWidth = parentRight - parentLeft;
int parentHeight = parentBottom - parentTop;
// Default position // Default position
if (cuePosition == Cue.DIMEN_UNSET) { if (cuePosition == Cue.DIMEN_UNSET) {
cuePositionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
cuePosition = 0.5f; cuePosition = 0.5f;
} }
@ -366,13 +370,25 @@ import com.google.android.exoplayer2.util.Util;
cueLine = 0.85f; cueLine = 0.85f;
} }
// Default width // Default width and height
if (cueSize == Cue.DIMEN_UNSET) { if (cueSize == Cue.DIMEN_UNSET) {
cueSize = 0.5f; // Scale up by height to be 10% of the parent's height
cueBitmapHeight = 0.1f;
float heightInParent = parentHeight * cueBitmapHeight;
float widthInParent = heightInParent * ((float) cueBitmap.getWidth() / cueBitmap.getHeight());
cueSize = widthInParent / parentWidth;
// If by the previous scaling the width exceeds 50% of the parent's width
// then scale back to 50% by width and adjust its height to keep the aspect ratio
if (cueSize > 0.5f) {
cueSize = 0.5f;
widthInParent = parentWidth * cueSize;
heightInParent = widthInParent * ((float) cueBitmap.getHeight() / cueBitmap.getWidth());
cueBitmapHeight = heightInParent / parentHeight;
}
} }
int parentWidth = parentRight - parentLeft;
int parentHeight = parentBottom - parentTop;
float anchorX = parentLeft + (parentWidth * cuePosition); float anchorX = parentLeft + (parentWidth * cuePosition);
float anchorY = parentTop + (parentHeight * cueLine); float anchorY = parentTop + (parentHeight * cueLine);
int width = Math.round(parentWidth * cueSize); int width = Math.round(parentWidth * cueSize);