Android Wing of mucchin | page 5: Home



How to get the camera preview image of Android Apps


How to get a preview of the camera as an image of the Android app?

The last of the camera shutter sound on Android is a continuation of.

In Android, is a way to save without the () takePicture, the camera image.
I try different colors, I was able to save as an image somehow, and I want to introduce, including horror stories.


Failure cases 1 getDrawingCache ()

In Android, whether there is such a useful API to be saved as a snapshot of the screen image? In terms of, I looked.
Then, we have the following methods.
getDrawingCache ()
This is the method of android.view.View class.

First, if you try using the () getDrawingCache, null was returned.
And read the API reference, because there was an explanation must enable the cache, I tried to run just before the following methods.
setDrawingCacheEnabled (true);

This, rather than null, is an instance of the Bitmap tentatively is now returned.
To handle an instance of it, but was not saved, was a black image file.
Also in the emulator, even Xperia.

I have tried with wrong usage of the API as Nde did not seem, would not do if other than the preview of the camera.
If you do the same thing on the screen or button to display only the usual image of the application has been properly stored.
Is the inference, and a preview of the camera, in View of the ordinary, the display buffer is something or different, perhaps, think getDrawingCache this (), the preview image of the camera would not be obtained, this method was abandoned.


Processing as a byte array that is passed in case 2 PreviewCallback failure

Android's camera functionality, you can configure an instance when performing operations such as processing and photography of the preview, to call back.
A preview of the camera, if you set a callback, it will be called back one by one, is PreviewCallback.
Specifically, it is a callback that you set in the following method.
setPreviewCallback (Camera.PreviewCallback cb)
This is the method of android.hardware.Camera class.
During the preview process, on a regular basis, () method is called an instance of type onPreviewFrame PreviewCallback that you set in the argument.
As an argument to this method, is to come is passed an array of type byte, this should have been in the preview image data.

However, this byte array, and it can not be converted to a Bitmap as ·.
For example, if you pass the following methods, this array, null is returned will cause.
decodeByteArray (byte [] data, int offset, int length)
Will be This is not a method of android.graphics.BitmapFactory class, that null is returned by this method, it is not a byte array format is correct.




Processing a byte array that is passed in the case PreviewCallback success

The previous method is, it should go do it to convert the byte array to something! And believing, I examined a little more sticky.
Then, there was a place where similar questions are asked.
You want turned into a bitmap byte data from the camera
You must tell me in a way that Mr. saikoro of this article.
OnPreviewFrame PreviewCallback type of instance (), but only an excerpt transform method. (If you do not know just this, please try to browse the source site.)


public void onPreviewFrame (byte [] data, Camera camera) {
final int width = getWidth (); final int height = getHeight width of / / preview (); int height / / Preview [] rgb = new int [(width * height)]; try an array of pixels of the / / ARGB8888 {
Bitmap bmp = Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888); bmp.setPixels conversion / / (rgb, 0, width; decodeYUV420SP creating empty bitmap (rgb, data, width, height) is / / ARGB8888 , 0, 0, width, height); set to a bitmap from pixel / / was converted

/ / ★
} Catch (Exception e) {
/ / Error}
}

public static final void decodeYUV420SP (int [] rgb, byte [] yuv420sp, int width, int height) {
final int frameSize = width * height;
for (int j = 0, yp = 0; j <height; j + +) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i <width; i + +, yp + +) {
int y = (0xff & ((int) yuv420sp [yp])) - 16;
if (y <0) y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp [uvp + +]) - 128;
u = (0xff & yuv420sp [uvp + +]) - 128;
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r <0) r = 0; else if (r> 262143) r = 262143;
if (g <0) g = 0; else if (g> 262143) g = 262143;
if (b <0) b = 0; else if (b> 262143) b = 262143;
rgb [yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
}


However, in this article, as far as actually to save the images are not delving.
Is part of the "★" above.
I think next time, and after thinking I would like to introduce my own way, save a sample of the processing of this part.
How to save pictures taken with the camera function of the Android app

3 4 5 6 7 ... 10 20 30 ... Last » Page 5 of eighty-one «First ... three four 5 6 seven ... 10 20 30 ... Last »