I want to make an application where users can select 3 gallery images. After click next button a video will be created with these 3 photos and user can save this video to sd card.
Try using animation set in android that can help you achieve what you are after it is called FrameAnimation, here is an example on how to use it:
FrameAnimation Example
or checkout below code snippet if it helps :
` final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
int randomNum = random.nextInt(6);
dice.setImageResource(images[randomNum]);
handler.postDelayed(this, 500);
}
}, 500);`
You can use jcodec SequenceEncoder to convert sequence of images to MP4 file.
Sample code :
import org.jcodec.api.awt.SequenceEncoder;
...
SequenceEncoder enc = new SequenceEncoder(new File("filename"));
// GOP size will be supported in 0.2
// enc.getEncoder().setKeyInterval(25);
for(...) {
BufferedImage image = ... // Obtain an image to encode
enc.encodeImage(image);
}
enc.finish();
It's a java library so it's easy to import it into Android project, you don't have to use NDK unlike ffmpeg.
Refer http://jcodec.org/ for sample code & downloads.
According to Abhishek V
Look here to see more
Related
I'm trying to ingest a mp4 file and make it a timelapse. It works with the code attached below. However, the output file has frame rate of 16*originalFrameRate. Since I don't intend to play it as a slow motion video I'd prefer to drop those redundant frames to make the output file smaller.
Movie inputMovie = MovieCreator.build(fileUri);
List<Track> videoTracks = new LinkedList<>();
for (Track track : inputMovie.getTracks()) {
if (track.getHandler().equals("vide")) {
videoTracks.add(track);
}
}
final int speedByFactorOf = 16;
Movie outputMovie = new Movie();
AppendTrack appendedTracks = new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()]));
outputMovie.addTrack(new WrappingTrack(appendedTracks) {
#Override
public long[] getSampleDurations() {
long[] l = super.getSampleDurations();
for (int i = 0; i < l.length; i++) {
l[i] /= speedByFactorOf;
}
return l;
}
});
BasicContainer out = (BasicContainer) new DefaultMp4Builder().build(outputMovie);
FileChannel fc = new RandomAccessFile("timelapse.mp4", "rw").getChannel();
out.writeContainer(fc);
fc.close();
out.close();
I was unable to find any examples of how to change the output frame rate.
As stated by #tarun-lalwani if the project you're referring to is https://github.com/sannies/mp4parser then it is only able to edit the MP4 container and NOT the video / audio / media etc. held within the container. Even if you could use metadata to accelerate the FPS by sixteen times, the file size would not become any smaller because all the frames would still be within the file (just shown for a shorter duration). You would need to use something like FFmpeg (e.g. via https://github.com/bramp/ffmpeg-cli-wrapper ) or some other programmatic video editor to do what you're describing thus only keeping every sixteenth frame of video so the video file actually becomes smaller.
TLDR; mp4parser is not the correct project for editing video (as opposed to metadata) and what you want to achieve sounds like it is beyond the scope of just fiddling with the container.
This question is related to this previously answered question Share Image on Android Application from Unity Game
My question is this:
Is there a way to share the image WITHOUT using a file? I'd like to remove the sd card read/write permission on Android. I'm not a Java programmer, so I'm having a heck of a time getting it working. None of my attempts have yielded anything.
"Programmer" answered the previous question with the below code, which works great for me. I basically want to use the variable imageBytes instead of the file. Is this even possible?
void takeScreenShotAndShare()
{
StartCoroutine(takeScreenshotAndSave());
}
private IEnumerator takeScreenshotAndSave()
{
string path = "";
yield return new WaitForEndOfFrame();
Texture2D screenImage = new Texture2D(Screen.width, Screen.height);
//Get Image from screen
screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenImage.Apply();
//Convert to png
byte[] imageBytes = screenImage.EncodeToPNG();
System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/GameOverScreenShot");
path = Application.persistentDataPath + "/GameOverScreenShot" + "/DiedScreenShot.png";
System.IO.File.WriteAllBytes(path, imageBytes);
StartCoroutine(shareScreenshot(path));
}
private IEnumerator shareScreenshot(string destination)
{
string ShareSubject = "Picture Share";
string shareLink = "Test Link" + "\nhttps://stackoverflow.com/questions/36512784/share-image-on-android-application-from-unity-game";
string textToShare = "Text To share";
Debug.Log(destination);
if (!Application.isEditor)
{
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + destination);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), textToShare + shareLink);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), ShareSubject);
intentObject.Call<AndroidJavaObject>("setType", "image/png");
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("startActivity", intentObject);
}
yield return null;
}
Unity3d share image on android
You can directly pass the byte array, no need to convert it to file -> uri -> string
Below are java code, but you can easily translate this to JNI wrapper calls
Bundle bundle = new Bundle();
bundle.putByteArray("byteArr", imageBytes); //put byte array to bundle!!!
Intent intent = new Intent(...);
intent.putExtra(bundle );
To retrieve the image byte array in another activity, use below code:
getIntent().getByteArrayExtra("byteArr");
If you wrap each above function calls into a corresponding JNI function, it would be very tedious. You can on the other hand:
Create an android library project
Add two static java functions, one is saveByteArrayToBundle(...), and the other is getByteArrayFromBundle(...)
Compile the android library into an aar file;
then put the aar file into the Assets/ folder (actually any subfolders will do)
Then call the two static java functions as you have done in the OP
Refer to below tutorials if you need to go through step-by-step:
Step-by-Step guide for developing Android Plugin for Unity3D (I)
Step-by-Step guide for developing Android Plugin for Unity3D (II)
I try to use VLCj to get access to web-cameras. I am using this code:
public static void main(String[] args) {
// Create player.
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
DirectMediaPlayer mediaPlayer = mediaPlayerFactory.newDirectMediaPlayer(
320, 240,
new RenderCallback() {
#Override
public void display(Memory arg0) {
// Do nothing.
}
});
// Options setup.
String[] options = new String[]{};
String mrl = "v4l2:///dev/video0"; // Linux
// Start preocessing.
mediaPlayer.startMedia(mrl, options);
BufferedImage bufImg;
for (int i = 0; i < 1000; ++i) {
bufImg = mediaPlayer.getSnapshot();
// Do something with BufferedImage...
// ...
}
// Stop precessing.
mediaPlayer.stop();
mediaPlayer = null;
System.out.println("Finish!");
}
And this code partially works -- I can get and work with BufferedImage, but:
I got an error in to output: [0x7f0a4c001268] main vout display error: Failed to set on top
When main loop is finished and camera was disabled program don't finished! I see Finish! message, but program not return control into IDE or console.
UPD:
I am using openSUSE 12.2 x64, VLC 2.0.3 installed and working properly for all video files, library VLCj 2.1.0.
This code working properly:
public static void main(String[] args) {
// Configure player factory.
String[] VLC_ARGS = {
"--intf", "dummy", // no interface
"--vout", "dummy", // we don't want video (output)
"--no-audio", // we don't want audio (decoding)
"--no-video-title-show", // nor the filename displayed
"--no-stats", // no stats
"--no-sub-autodetect-file", // we don't want subtitles
"--no-inhibit", // we don't want interfaces
"--no-disable-screensaver", // we don't want interfaces
"--no-snapshot-preview", // no blending in dummy vout
};
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(VLC_ARGS);
// Create player.
HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
// Select input device.
String mrl = "v4l2:///dev/video0"; // Linux
// Start processing.
mediaPlayer.startMedia(mrl);
BufferedImage bufImg;
for (int i = 0; i < 1000; ++i) {
bufImg = mediaPlayer.getSnapshot();
// Do something with BufferedImage...
// ...
}
// Stop processing.
mediaPlayer.stop();
// Finish program.
mediaPlayer.release();
mediaPlayerFactory.release();
}
Re your native window: VLCj opens a shared instance to the VLC library.
A headless media palyer is NOT intended to have a video or audio output!
In fact, if you need anything to play (and not to stream to anywhere else) you need to create either an output window or use a direct media player (may be much more complicated)
So, if a headless player needs to play something it opens a native window to perform the playback!
Source: http://www.capricasoftware.co.uk/wiki/index.php?title=Vlcj_Media_Players
Re the error: the video display component MUST be the top component of the panel, window or whereever it is added to. Otherwise it will throw the error
main vout display error: Failed to set on top
Furthermore, if you put anything over the component it will destroy the video output which won't work anymore!
Anyway, I don't know how the DirectMediaPlayer works in detail but VLCj has some weird behaviour... Maybe getSnapshot() needs a video display component but I'm not sure.
Re your not finishing program: you join to finish your own thread. This can't work because your thread "sleeps" until the other thread who is waited for has been terminated but as this is your own thread it "sleeps" and won't terminate.
You can test this behaviour with this short code in a main method:
System.out.println("Test start");
Thread.currentThread().join();
System.out.println("Test stop");
You will NEVER reach the "Test stop" statement.
I am implementing a QR code scanner for blackberry devices and I am using ZXing libraries to do so. This is for os 6+ by the way. The problem I am having is that sometimes, only sometimes, when the camera opens up to prepare scanning, the device will freeze and do a full reboot...
Otherwise it works most of the time, I am able to scan and decode the qr codes etc. However is just seems like it occasionally feels like crashing for no reason. I do not know if it is something with the camera or something in my code, but I will provide the code.
public void scanBarcode() {
// First we create a hashtable to hold all of the hints that we can
// give the API about how we want to scan a barcode to improve speed
// and accuracy.
Hashtable hints = new Hashtable();
// The first thing going in is a list of formats. We could look for
// more than one at a time, but it's much slower.
Vector formats = new Vector();
formats.addElement(BarcodeFormat.QR_CODE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);
// We will also use the "TRY_HARDER" flag to make sure we get an
// accurate scan
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
// We create a new decoder using those hints
BarcodeDecoder decoder = new BarcodeDecoder(hints);
// Finally we can create the actual scanner with a decoder and a
// listener that will handle the data stored in the barcode. We put
// that in our view screen to handle the display.
try {
_scanner = new BarcodeScanner(decoder, new MyBarcodeDecoderListener());
_barcodeScreen = new MyBarcodeScannerViewScreen(_scanner);
} catch (Exception e) {
return;
}
// If we get here, all the barcode scanning infrastructure should be set
// up, so all we have to do is start the scan and display the viewfinder
try {
_scanner.stopScan();
_scanner.getPlayer().start();
_scanner.startScan();
UiApplication.getUiApplication().pushScreen(_barcodeScreen);
} catch (Exception e) {
}
}
/***
* MyBarcodeDecoderListener
* <p>
* This BarcodeDecoverListener implementation tries to open any data encoded
* in a barcode in the browser.
*
* #author PBernhardt
*
**/
private class MyBarcodeDecoderListener implements BarcodeDecoderListener {
public void barcodeDecoded(final String rawText) {
//UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
UtilityDecoder.saveToHistory(rawText);
try {
UtilityDecoder.distributeBarcode(rawText);
} catch (PIMException e) {
}
}
}
I basically call scanBarcode() when I click on a button on a toolbar.
Can anyone tell me if my code is the problem, or the device, or something else? Thanks in advance for any help provided!
Try this link:
Scan Any Type of Barcode Image which Supports Blackberry
See this forun link and see the discussions of that link. Surely, you will get overall concept of barcode scanning and you will also get the Implemention of QRCode in version 5.0
Any type of Barcode scaning in 5.0
In Android, Using ZXing we can scan a QR code through phone camera and decode it.
But, in my scenario, the QR code image is stored in the phone itself and I need to decode it.
Is there anyway to decode a QR image in this manner?
You can use ZXing code for this.
Check out DecodeHandler.java.
You can simply use the Mobile Vision API for decoding a QR Code from Image.It is very accurate and can detect more than one Qr code over image.
You have to include the following library inorder to use Mobile Vision API :
compile 'com.google.android.gms:play-services-vision:9.6.1'
BarcodeDetector detector =
new BarcodeDetector.Builder(context)
.setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE)
.build();
if(!detector.isOperational()){
Log.d("QR_READ","Could not set up the detector!");
}
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<Barcode> barcodes = detector.detect(frame);
Log.d("QR_READ","-barcodeLength-"+barcodes.size());
Barcode thisCode=null;
if(barcodes.size()==0){
Log.d("QR_VALUE","--NODATA");
}
else if(barcodes.size()==1){
thisCode = barcodes.valueAt(0);
Log.d("QR_VALUE","--"+thisCode.rawValue);
}
else{
for(int iter=0;iter<barcodes.size();iter++) {
thisCode = barcodes.valueAt(iter);
Log.d("QR_VALUE","--"+thisCode.rawValue);
}
}