Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:23:01+00:00 2026-06-11T04:23:01+00:00

Ok so my question is pretty much identical to this: Converting preview frame to

  • 0

Ok so my question is pretty much identical to this:
Converting preview frame to bitmap

However his answer is no good, and trying to use it doesn’t solve my problem.

So what I’m trying to do at the moment is to send each frame as a bitmap to a method to detect if there are any faces, but first I need to create a bitmap which means I have to use the decodeYUV420sp method, which doesn’t seem to be working properly and all my images just come out as a green and yellow tie dye looking image. Here is my code:

This is from onPreviewFrame:

    Parameters parameters = cam.getParameters(); 

    Integer width = parameters.getPreviewSize().width;
    Integer height = parameters.getPreviewSize().height;

    Log.i("preview size: ", String.valueOf(width) + "x" + String.valueOf(height));
    int[] mIntArray = new int[width*height];

    // Decode Yuv data to integer array
    decodeYUV420SP(mIntArray, data, width, height);

    //Initialize the bitmap, with the replaced color  
    Bitmap bmp = Bitmap.createBitmap(mIntArray, width, height, Bitmap.Config.ARGB_8888);  

    saveImage(bmp);

This is decodeYUV method:

    static public void decodeYUV420SP(int[] rgba, 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);
            // rgba, divide 2^10 ( >> 10)
            rgba[yp] = ((r << 14) & 0xff000000) | ((g << 6) & 0xff0000)
                    | ((b >> 2) | 0xff00);
        }
    }
    }

and this is the method I’m calling to save the bitmaps to see what they look like:

       private void saveImage(Bitmap bmp) {

      File myDir=new File("/sdcard/saved_images");
      myDir.mkdirs();
      Random generator = new Random();
      int n = 10000;
      n = generator.nextInt(n);
      String fname = "Image-"+ n +".jpg";
      File file = new File (myDir, fname);
      if (file.exists ()) file.delete (); 
      try {
           FileOutputStream   out = new FileOutputStream(file);
           bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

       } catch (Exception e) {
           e.printStackTrace();
      }
    }

Here is a resulting image:
https://docs.google.com/drawings/d/1kyIvb4oHHInW_c71mjfFSVCxVopBgBWX3k1OR_nMgRA/edit

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:23:02+00:00Added an answer on June 11, 2026 at 4:23 am

    Ok so the problem was the decodeYUV method which I got from a different stackoverflow post here:
    Converting YUV->RGB(Image processing)->YUV during onPreviewFrame in android? didn’t quite work.

    But I replaced that with what I think must be the original decodeYUV method from here:
    http://code.google.com/p/android/issues/detail?id=823
    an

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue pretty much identical to this SO question except with a
The question pretty much says it all. What supported JVM GC should we use
This question does pretty much what I want to accomplish, but my table is
The question pretty much says it all, I've been looking around for an answer
This question is pretty much similar to this one , but for SQL Server
The question pretty much sums this up, but I'll provide some more details. I
This question is pretty much the same as this .Net question exept for java.
The question pretty much says it all. I'm not sure how to do this
The question pretty much says it all. I'm trying to create a submission form
This question is pretty much a duplicate but the linked issue was never really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.